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"
59 /* #include <assert.h> -- Now using assert in ircd_log.h */
69 /** Global list of all ConfItem structures. */
70 struct ConfItem *GlobalConfList;
71 /** Count of items in #GlobalConfList. */
73 /** Global list of service mappings. */
74 struct s_map *GlobalServiceMapList;
75 /** Global list of channel quarantines. */
76 struct qline *GlobalQuarantineList;
78 /** Current line number in scanner input. */
81 /** Configuration information for #me. */
82 struct LocalConf localConf;
83 /** Global list of connection rules. */
84 struct CRuleConf* cruleConfList;
85 /** Global list of K-lines. */
86 struct DenyConf* denyConfList;
88 /** Tell a user that they are banned, dumping the message from a file.
89 * @param sptr Client being rejected
90 * @param filename Send this file's contents to \a sptr
92 static void killcomment(struct Client* sptr, const char* filename)
99 if (NULL == (file = fbopen(filename, "r"))) {
100 send_reply(sptr, ERR_NOMOTD);
101 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
102 ":Connection from your host is refused on this server.");
106 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
107 while (fbgets(line, sizeof(line) - 1, file)) {
108 char* end = line + strlen(line);
111 if ('\n' == *end || '\r' == *end)
116 send_reply(sptr, RPL_MOTD, line);
118 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
119 ":Connection from your host is refused on this server.");
123 /** Allocate a new struct ConfItem and link it to #GlobalConfList.
124 * @return Newly allocated structure.
126 struct ConfItem* make_conf(int type)
128 struct ConfItem* aconf;
130 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
135 memset(aconf, 0, sizeof(struct ConfItem));
136 aconf->status = type;
137 aconf->next = GlobalConfList;
138 GlobalConfList = aconf;
142 /** Free a struct ConfItem and any resources it owns.
143 * @param aconf Item to free.
145 void free_conf(struct ConfItem *aconf)
147 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
148 aconf->host ? aconf->host : "*",
149 aconf->name ? aconf->name : "*",
150 aconf->address.port));
151 if (aconf->dns_pending)
152 delete_resolver_queries(aconf);
155 memset(aconf->passwd, 0, strlen(aconf->passwd));
156 MyFree(aconf->passwd);
164 /** Disassociate configuration from the client.
165 * @param cptr Client to operate on.
166 * @param aconf ConfItem to detach.
168 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
175 assert(0 < aconf->clients);
177 lp = &(cli_confs(cptr));
180 if ((*lp)->value.aconf == aconf) {
181 if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
184 assert(0 < aconf->clients);
185 if (0 == --aconf->clients && IsIllegal(aconf))
196 /** Parse a user\@host mask into username and host or IP parts.
197 * If \a host contains no username part, set \a aconf->username to
198 * NULL. If the host part of \a host looks like an IP mask, set \a
199 * aconf->addrbits and \a aconf->address to match. Otherwise, set
200 * \a aconf->host, and set \a aconf->addrbits to -1.
201 * @param[in,out] aconf Configuration item to set.
202 * @param[in] host user\@host mask to parse.
204 void conf_parse_userhost(struct ConfItem *aconf, char *host)
207 unsigned char addrbits;
209 MyFree(aconf->username);
211 host_part = strchr(host, '@');
214 DupString(aconf->username, host);
217 aconf->username = NULL;
220 DupString(aconf->host, host_part);
221 if (ipmask_parse(aconf->host, &aconf->address.addr, &addrbits))
222 aconf->addrbits = addrbits;
224 aconf->addrbits = -1;
228 /** Copies a completed DNS query into its ConfItem.
229 * @param vptr Pointer to struct ConfItem for the block.
230 * @param hp DNS reply, or NULL if the lookup failed.
232 static void conf_dns_callback(void* vptr, struct DNSReply* hp)
234 struct ConfItem* aconf = (struct ConfItem*) vptr;
236 aconf->dns_pending = 0;
238 memcpy(&aconf->address.addr, &hp->addr, sizeof(aconf->address.addr));
243 /** Start a nameserver lookup of the conf host. If the conf entry is
244 * currently doing a lookup, do nothing.
245 * @param aconf ConfItem for which to start a request.
247 static void conf_dns_lookup(struct ConfItem* aconf)
249 if (!aconf->dns_pending) {
250 char buf[HOSTLEN + 1];
251 struct DNSQuery query;
253 query.callback = conf_dns_callback;
254 host_from_uh(buf, aconf->host, HOSTLEN);
257 gethost_byname(buf, &query);
258 aconf->dns_pending = 1;
263 /** Start lookups of all addresses in the conf line. The origin must
264 * be a numeric IP address. If the remote host field is not an IP
265 * address, start a DNS lookup for it.
266 * @param aconf Connection to do lookups for.
269 lookup_confhost(struct ConfItem *aconf)
271 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
272 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
273 aconf->host, aconf->name));
276 if (aconf->origin_name
277 && !ircd_aton(&aconf->origin.addr, aconf->origin_name)) {
278 Debug((DEBUG_ERROR, "Origin name error: (%s) (%s)",
279 aconf->origin_name, aconf->name));
282 * Do name lookup now on hostnames given and store the
283 * ip numbers in conf structure.
285 if (IsIP6Char(*aconf->host)) {
286 if (!ircd_aton(&aconf->address.addr, aconf->host)) {
287 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
288 aconf->host, aconf->name));
292 conf_dns_lookup(aconf);
295 /** Find a server by name or hostname.
296 * @param name Server name to find.
297 * @return Pointer to the corresponding ConfItem, or NULL if none exists.
299 struct ConfItem* conf_find_server(const char* name)
301 struct ConfItem* conf;
304 for (conf = GlobalConfList; conf; conf = conf->next) {
305 if (CONF_SERVER == conf->status) {
307 * Check first servernames, then try hostnames.
308 * XXX - match returns 0 if there _is_ a match... guess they
309 * haven't decided what true is yet
311 if (0 == match(name, conf->name))
318 /** Evaluate connection rules.
319 * @param name Name of server to check
320 * @param mask Filter for CRule types (only consider if type & \a mask != 0).
321 * @return Name of rule that forbids the connection; NULL if no prohibitions.
323 const char* conf_eval_crule(const char* name, int mask)
325 struct CRuleConf* p = cruleConfList;
328 for ( ; p; p = p->next) {
329 if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
330 if (crule_eval(p->node))
337 /** Remove all conf entries from the client except those which match
338 * the status field mask.
339 * @param cptr Client to operate on.
340 * @param mask ConfItem types to keep.
342 void det_confs_butmask(struct Client* cptr, int mask)
348 for (link = cli_confs(cptr); link; link = next) {
350 if ((link->value.aconf->status & mask) == 0)
351 detach_conf(cptr, link->value.aconf);
355 /** Check client limits and attach Client block.
356 * If there are more connections from the IP than \a aconf->maximum
357 * allows, return ACR_TOO_MANY_FROM_IP. Otherwise, attach \a aconf to
359 * @param cptr Client getting \a aconf.
360 * @param aconf Configuration item to attach.
361 * @return Authorization check result.
363 static enum AuthorizationCheckResult
364 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
366 if (IPcheck_nr(cptr) > aconf->maximum)
367 return ACR_TOO_MANY_FROM_IP;
368 return attach_conf(cptr, aconf);
371 /** Find the first (best) Client block to attach.
372 * @param cptr Client for whom to check rules.
373 * @return Authorization check result.
375 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
377 struct ConfItem* aconf;
382 hp = cli_dns_reply(cptr);
383 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
384 if (aconf->status != CONF_CLIENT)
386 if (aconf->address.port && aconf->address.port != cli_listener(cptr)->addr.port)
388 if (aconf->username) {
389 SetFlag(cptr, FLAG_DOID);
390 if (match(aconf->username, cli_username(cptr)))
393 if (aconf->host && (!hp || match(aconf->host, hp->h_name)))
395 if ((aconf->addrbits >= 0)
396 && !ipmask_check(&cli_ip(cptr), &aconf->address.addr, aconf->addrbits))
398 return check_limit_and_attach(cptr, aconf);
400 return ACR_NO_AUTHORIZATION;
403 /** Check whether a particular ConfItem is already attached to a
405 * @param aconf ConfItem to search for
406 * @param cptr Client to check
407 * @return Non-zero if \a aconf is attached to \a cptr, zero if not.
409 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
413 for (lp = cli_confs(cptr); lp; lp = lp->next) {
414 if (lp->value.aconf == aconf)
420 /** Associate a specific configuration entry to a *local* client (this
421 * is the one which used in accepting the connection). Note, that this
422 * automaticly changes the attachment if there was an old one...
423 * @param cptr Client to attach \a aconf to
424 * @param aconf ConfItem to attach
425 * @return Authorization check result.
427 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
431 if (is_attached(aconf, cptr))
432 return ACR_ALREADY_AUTHORIZED;
433 if (IsIllegal(aconf))
434 return ACR_NO_AUTHORIZATION;
435 if ((aconf->status & (CONF_OPERATOR | CONF_CLIENT)) &&
436 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
437 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
439 lp->next = cli_confs(cptr);
440 lp->value.aconf = aconf;
441 cli_confs(cptr) = lp;
443 if (aconf->status & CONF_CLIENT_MASK)
448 /** Return our LocalConf configuration structure.
449 * @return A pointer to #localConf.
451 const struct LocalConf* conf_get_local(void)
456 /** Attach ConfItems to a client if the name passed matches that for
457 * the ConfItems or is an exact match for them.
458 * @param cptr Client getting the ConfItem attachments.
459 * @param name Filter to match ConfItem::name.
460 * @param statmask Filter to limit ConfItem::status.
461 * @return First ConfItem attached to \a cptr.
463 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
466 struct ConfItem* tmp;
467 struct ConfItem* first = NULL;
471 if (HOSTLEN < strlen(name))
474 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
475 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
476 assert(0 != tmp->name);
477 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
478 if (ACR_OK == attach_conf(cptr, tmp) && !first)
486 /** Attach ConfItems to a client if the host passed matches that for
487 * the ConfItems or is an exact match for them.
488 * @param cptr Client getting the ConfItem attachments.
489 * @param host Filter to match ConfItem::host.
490 * @param statmask Filter to limit ConfItem::status.
491 * @return First ConfItem attached to \a cptr.
493 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
496 struct ConfItem* tmp;
497 struct ConfItem* first = 0;
500 if (HOSTLEN < strlen(host))
503 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
504 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
505 assert(0 != tmp->host);
506 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
507 if (ACR_OK == attach_conf(cptr, tmp) && !first)
515 /** Find a ConfItem that has the same name and user+host fields as
516 * specified. Requires an exact match for \a name.
517 * @param name Name to match
518 * @param cptr Client to match against
519 * @param statmask Filter for ConfItem::status
520 * @return First found matching ConfItem.
522 struct ConfItem* find_conf_exact(const char* name, struct Client *cptr, int statmask)
524 struct ConfItem *tmp;
526 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
527 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
528 0 != ircd_strcmp(tmp->name, name))
531 && (EmptyString(cli_username(cptr))
532 || match(tmp->username, cli_username(cptr))))
534 if (tmp->addrbits < 0)
536 if (match(tmp->host, cli_sockhost(cptr)))
539 else if (!ipmask_check(&cli_ip(cptr), &tmp->address.addr, tmp->addrbits))
541 if ((tmp->status & CONF_OPERATOR)
542 && (tmp->clients >= MaxLinks(tmp->conn_class)))
549 /** Find a ConfItem from a list that has a name that matches \a name.
550 * @param lp List to search in.
551 * @param name Filter for ConfItem::name field; matches either exactly
553 * @param statmask Filter for ConfItem::status.
554 * @return First matching ConfItem from \a lp.
556 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
559 struct ConfItem* tmp;
562 if (HOSTLEN < strlen(name))
565 for (; lp; lp = lp->next) {
566 tmp = lp->value.aconf;
567 if (0 != (tmp->status & statmask)) {
568 assert(0 != tmp->name);
569 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
576 /** Find a ConfItem from a list that has a host that matches \a host.
577 * @param lp List to search in.
578 * @param host Filter for ConfItem::host field; matches as a glob.
579 * @param statmask Filter for ConfItem::status.
580 * @return First matching ConfItem from \a lp.
582 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
585 struct ConfItem* tmp = NULL;
588 if (HOSTLEN < strlen(host))
591 for (; lp; lp = lp->next) {
592 tmp = lp->value.aconf;
593 if (0 != (tmp->status & statmask)) {
594 assert(0 != tmp->host);
595 if (0 == match(tmp->host, host))
602 /** Find a ConfItem from a list that has an address equal to \a ip.
603 * @param lp List to search in.
604 * @param ip Filter for ConfItem::address field; matches exactly.
605 * @param statmask Filter for ConfItem::status.
606 * @return First matching ConfItem from \a lp.
608 struct ConfItem* find_conf_byip(struct SLink* lp, const struct irc_in_addr* ip,
611 struct ConfItem* tmp;
613 for (; lp; lp = lp->next) {
614 tmp = lp->value.aconf;
615 if (0 != (tmp->status & statmask)
616 && !irc_in_addr_cmp(&tmp->address.addr, ip))
622 /** Free all CRules from #cruleConfList. */
623 void conf_erase_crule_list(void)
625 struct CRuleConf* next;
626 struct CRuleConf* p = cruleConfList;
628 for ( ; p; p = next) {
630 crule_free(&p->node);
638 /** Return #cruleConfList.
639 * @return #cruleConfList
641 const struct CRuleConf* conf_get_crule_list(void)
643 return cruleConfList;
646 /** Free all deny rules from #denyConfList. */
647 void conf_erase_deny_list(void)
649 struct DenyConf* next;
650 struct DenyConf* p = denyConfList;
651 for ( ; p; p = next) {
661 /** Return #denyConfList.
662 * @return #denyConfList
664 const struct DenyConf* conf_get_deny_list(void)
669 /** Find any existing quarantine for the named channel.
670 * @param chname Channel name to search for.
671 * @return Reason for channel's quarantine, or NULL if none exists.
674 find_quarantine(const char *chname)
678 for (qline = GlobalQuarantineList; qline; qline = qline->next)
679 if (!ircd_strcmp(qline->chname, chname))
680 return qline->reason;
684 /** Free all qline structs from #GlobalQuarantineList. */
685 void clear_quarantines(void)
688 while ((qline = GlobalQuarantineList))
690 GlobalQuarantineList = qline->next;
691 MyFree(qline->reason);
692 MyFree(qline->chname);
697 /** When non-zero, indicates that a configuration error has been seen in this pass. */
698 static int conf_error;
699 /** When non-zero, indicates that the configuration file was loaded at least once. */
700 static int conf_already_read;
702 extern void yyparse(void);
703 extern void init_lexer(void);
705 /** Read configuration file.
706 * @return Zero on failure, non-zero on success. */
707 int read_configuration_file(void)
710 feature_unmark(); /* unmark all features for resetting later */
711 /* Now just open an fd. The buffering isn't really needed... */
716 feature_mark(); /* reset unmarked features */
717 conf_already_read = 1;
721 /** Report an error message about the configuration file.
722 * @param msg The error to report.
725 yyerror(const char *msg)
727 sendto_opmask_butone(0, SNO_ALL, "Config file parse error line %d: %s",
729 log_write(LS_CONFIG, L_ERROR, 0, "Config file parse error line %d: %s",
731 if (!conf_already_read)
732 fprintf(stderr, "Config file parse error line %d: %s\n", lineno, msg);
736 /** Attach CONF_UWORLD items to a server and everything attached to it. */
738 attach_conf_uworld(struct Client *cptr)
742 attach_confs_byhost(cptr, cli_name(cptr), CONF_UWORLD);
743 for (lp = cli_serv(cptr)->down; lp; lp = lp->next)
744 attach_conf_uworld(lp->value.cptr);
747 /** Reload the configuration file.
748 * @param cptr Client that requested rehash (if a signal, &me).
749 * @param sig Type of rehash (0 = oper-requested, 1 = signal, 2 =
750 * oper-requested but do not restart resolver)
751 * @return CPTR_KILLED if any client was K/G-lined because of the
752 * rehash; otherwise 0.
754 int rehash(struct Client *cptr, int sig)
756 struct ConfItem** tmp = &GlobalConfList;
757 struct ConfItem* tmp2;
758 struct Client* acptr;
764 sendto_opmask_butone(0, SNO_OLDSNO,
765 "Got signal SIGHUP, reloading ircd conf. file");
767 while ((tmp2 = *tmp)) {
770 * Configuration entry is still in use by some
771 * local clients, cannot delete it--mark it so
772 * that it will be deleted when the last client
775 if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
781 tmp2->status |= CONF_ILLEGAL;
788 conf_erase_crule_list();
789 conf_erase_deny_list();
793 * delete the juped nicks list
803 mark_listeners_closing();
804 iauth_mark_closing();
806 read_configuration_file();
808 log_reopen(); /* reopen log files */
810 iauth_close_unused();
812 class_delete_marked(); /* unless it fails */
815 * Flush out deleted I and P lines although still in use.
817 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
818 if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
828 for (i = 0; i <= HighestFd; i++) {
829 if ((acptr = LocalClientArray[i])) {
830 assert(!IsMe(acptr));
832 det_confs_butmask(acptr, ~(CONF_UWORLD | CONF_ILLEGAL));
833 /* Because admin's are getting so uppity about people managing to
834 * get past K/G's etc, we'll "fix" the bug by actually explaining
837 if ((found_g = find_kill(acptr))) {
838 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
839 found_g == -2 ? "G-line active for %s%s" :
840 "K-line active for %s%s",
841 IsUnknown(acptr) ? "Unregistered Client ":"",
842 get_client_name(acptr, SHOW_IP));
843 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
844 "K-lined") == CPTR_KILLED)
850 attach_conf_uworld(&me);
855 /** Read configuration file for the very first time.
856 * @return Non-zero on success, zero on failure.
861 if (read_configuration_file()) {
863 * make sure we're sane to start if the config
864 * file read didn't get everything we need.
865 * XXX - should any of these abort the server?
866 * TODO: add warning messages
868 if (0 == localConf.name || 0 == localConf.numeric)
873 if (0 == localConf.location1)
874 DupString(localConf.location1, "");
875 if (0 == localConf.location2)
876 DupString(localConf.location2, "");
877 if (0 == localConf.contact)
878 DupString(localConf.contact, "");
885 /** Searches for a K/G-line for a client. If one is found, notify the
886 * user and disconnect them.
887 * @param cptr Client to search for.
888 * @return 0 if client is accepted; -1 if client was locally denied
889 * (K-line); -2 if client was globally denied (G-line).
891 int find_kill(struct Client *cptr)
895 const char* realname;
896 struct DenyConf* deny;
897 struct Gline* agline = NULL;
904 host = cli_sockhost(cptr);
905 name = cli_user(cptr)->username;
906 realname = cli_info(cptr);
908 assert(strlen(host) <= HOSTLEN);
909 assert((name ? strlen(name) : 0) <= HOSTLEN);
910 assert((realname ? strlen(realname) : 0) <= REALLEN);
912 /* 2000-07-14: Rewrote this loop for massive speed increases.
915 for (deny = denyConfList; deny; deny = deny->next) {
916 if (0 != match(deny->usermask, name))
919 if (EmptyString(deny->hostmask))
922 if (deny->flags & DENY_FLAGS_REALNAME) { /* K: by real name */
923 if (0 == match(deny->hostmask + 2, realname))
925 } else if (deny->flags & DENY_FLAGS_IP) { /* k: by IP */
927 char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
928 Debug((DEBUG_DEBUG, "ip: %s network: %s/%u",
929 ircd_ntoa_r(tbuf1, &cli_ip(cptr)), ircd_ntoa_r(tbuf2, &deny->address), deny->bits));
931 if (ipmask_check(&cli_ip(cptr), &deny->address, deny->bits))
934 else if (0 == match(deny->hostmask, host))
938 if (EmptyString(deny->message))
939 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
940 ":Connection from your host is refused on this server.");
942 if (deny->flags & DENY_FLAGS_FILE)
943 killcomment(cptr, deny->message);
945 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
948 else if ((agline = gline_lookup(cptr, 0))) {
951 * added a check against the user's IP address to find_gline() -Kev
953 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
964 /** Attempt to attach Client blocks to \a cptr. If attach_iline()
965 * fails for the client, emit a debugging message.
966 * @param cptr Client to check for access.
967 * @return Access check result.
969 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
971 enum AuthorizationCheckResult acr = ACR_OK;
973 if ((acr = attach_iline(cptr))) {
974 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
975 cli_name(cptr), cli_sockhost(cptr)));
981 /** Check access for a server given its name (passed in cptr struct).
982 * Must check for all C/N lines which have a name which matches the
983 * name given and a host which matches. A host alias which is the
984 * same as the server name is also acceptable in the host field of a
986 * @param cptr Peer server to check.
987 * @return 0 if accepted, -1 if access denied.
989 int conf_check_server(struct Client *cptr)
991 struct ConfItem* c_conf = NULL;
994 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
995 cli_name(cptr), cli_sockhost(cptr)));
997 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
998 Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
1001 lp = cli_confs(cptr);
1003 * We initiated this connection so the client should have a C and N
1004 * line already attached after passing through the connect_server()
1007 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1008 c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1010 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1012 det_confs_butmask(cptr, 0);
1018 if (cli_dns_reply(cptr)) {
1019 struct DNSReply* hp = cli_dns_reply(cptr);
1020 const char* name = hp->h_name;
1022 * If we are missing a C or N line from above, search for
1023 * it under all known hostnames we have for this ip#.
1025 if ((c_conf = find_conf_byhost(lp, hp->h_name, CONF_SERVER)))
1026 ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1028 c_conf = find_conf_byip(lp, &hp->addr, CONF_SERVER);
1032 * Check for C lines with the hostname portion the ip number
1033 * of the host the server runs on. This also checks the case where
1034 * there is a server connecting from 'localhost'.
1036 c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1040 * Attach by IP# only if all other checks have failed.
1041 * It is quite possible to get here with the strange things that can
1042 * happen when using DNS in the way the irc server does. -avalon
1045 c_conf = find_conf_byip(lp, &cli_ip(cptr), CONF_SERVER);
1047 * detach all conf lines that got attached by attach_confs()
1049 det_confs_butmask(cptr, 0);
1051 * if no Connect block, then deny access
1054 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1055 cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1059 * attach the Connect block to the client structure for later use.
1061 attach_conf(cptr, c_conf);
1063 if (!irc_in_addr_valid(&c_conf->address.addr))
1064 memcpy(&c_conf->address.addr, &cli_ip(cptr), sizeof(c_conf->address.addr));
1066 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]",
1067 cli_name(cptr), cli_sockhost(cptr)));