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.
32 #include "ircd_alloc.h"
33 #include "ircd_chattr.h"
35 #include "ircd_reply.h"
36 #include "ircd_snprintf.h"
37 #include "ircd_string.h"
50 #include "sprintf_irc.h"
56 #include <arpa/inet.h>
67 #define INADDR_NONE 0xffffffff
70 struct ConfItem* GlobalConfList = 0;
71 int GlobalConfCount = 0;
72 struct MotdItem* motd = NULL;
73 struct MotdItem* rmotd = NULL;
74 struct TRecord* tdata = NULL;
77 static struct LocalConf localConf;
78 static struct MotdConf* motdConfList;
79 static struct CRuleConf* cruleConfList;
82 * output the reason for being k lined from a file - Mmmm
84 * parv is the sender prefix
85 * filename is the file that is to be output to the K lined client
87 static void killcomment(struct Client *sptr, char *parv, char *filename)
95 if (NULL == (file = fbopen(filename, "r"))) {
96 send_reply(sptr, ERR_NOMOTD);
97 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
98 ":Connection from your host is refused on this server.");
102 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
103 while (fbgets(line, sizeof(line) - 1, file)) {
104 if ((tmp = strchr(line, '\n')))
106 if ((tmp = strchr(line, '\r')))
108 send_reply(sptr, RPL_MOTD, line);
110 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
111 ":Connection from your host is refused on this server.");
115 struct ConfItem* make_conf(void)
117 struct ConfItem* aconf;
119 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
124 aconf->status = CONF_ILLEGAL;
126 aconf->ipnum.s_addr = INADDR_NONE;
132 aconf->dns_pending = 0;
133 aconf->confClass = 0;
138 void delist_conf(struct ConfItem *aconf)
140 if (aconf == GlobalConfList)
141 GlobalConfList = GlobalConfList->next;
143 struct ConfItem *bconf;
145 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
147 bconf->next = aconf->next;
152 void free_conf(struct ConfItem *aconf)
154 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
155 aconf->host ? aconf->host : "*",
156 aconf->name ? aconf->name : "*",
158 if (aconf->dns_pending)
159 delete_resolver_queries(aconf);
162 memset(aconf->passwd, 0, strlen(aconf->passwd));
163 MyFree(aconf->passwd);
172 * conf_dns_callback - called when resolver query finishes
173 * if the query resulted in a successful search, hp will contain
174 * a non-null pointer, otherwise hp will be null.
175 * if successful save hp in the conf item it was called with
177 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
179 struct ConfItem* aconf = (struct ConfItem*) vptr;
180 aconf->dns_pending = 0;
182 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
186 * conf_dns_lookup - do a nameserver lookup of the conf host
187 * if the conf entry is currently doing a ns lookup do nothing, otherwise
188 * if the lookup returns a null pointer, set the conf dns_pending flag
190 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
192 struct DNSReply* dns_reply = 0;
193 if (!aconf->dns_pending) {
194 char buf[HOSTLEN + 1];
195 struct DNSQuery query;
197 query.callback = conf_dns_callback;
198 host_from_uh(buf, aconf->host, HOSTLEN);
201 if (0 == (dns_reply = gethost_byname(buf, &query)))
202 aconf->dns_pending = 1;
211 * Do (start) DNS lookups of all hostnames in the conf line and convert
212 * an IP addresses in a.b.c.d number for to IP#s.
214 static void lookup_confhost(struct ConfItem *aconf)
216 struct DNSReply* reply;
218 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
219 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
220 aconf->host, aconf->name));
224 * Do name lookup now on hostnames given and store the
225 * ip numbers in conf structure.
227 if (IsDigit(*aconf->host)) {
229 * rfc 1035 sez host names may not start with a digit
230 * XXX - this has changed code needs to be updated
232 aconf->ipnum.s_addr = inet_addr(aconf->host);
233 if (INADDR_NONE == aconf->ipnum.s_addr) {
234 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
235 aconf->host, aconf->name));
238 else if ((reply = conf_dns_lookup(aconf)))
239 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
243 * conf_find_server - find a server by name or hostname
244 * returns a server conf item pointer if found, 0 otherwise
246 * NOTE: at some point when we don't have to scan the entire
247 * list it may be cheaper to look for server names and host
248 * names in separate loops (original code did it that way)
250 struct ConfItem* conf_find_server(const char* name)
252 struct ConfItem* conf;
255 for (conf = GlobalConfList; conf; conf = conf->next) {
256 if (CONF_SERVER == conf->status) {
258 * Check first servernames, then try hostnames.
259 * XXX - match returns 0 if there _is_ a match... guess they
260 * haven't decided what true is yet
262 if (0 == match(name, conf->name))
270 * conf_eval_crule - evaluate connection rules
271 * returns the name of the rule triggered if found, 0 otherwise
273 * Evaluate connection rules... If no rules found, allow the
274 * connect. Otherwise stop with the first true rule (ie: rules
275 * are ored together. Oper connects are effected only by D
276 * lines (CRULE_ALL) not d lines (CRULE_AUTO).
278 const char* conf_eval_crule(const char* name, int mask)
280 struct CRuleConf* p = cruleConfList;
283 for ( ; p; p = p->next) {
284 if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
285 if (crule_eval(p->node))
293 * Remove all conf entries from the client except those which match
294 * the status field mask.
296 void det_confs_butmask(struct Client *cptr, int mask)
298 struct SLink *tmp, *tmp2;
300 for (tmp = cptr->confs; tmp; tmp = tmp2) {
302 if ((tmp->value.aconf->status & mask) == 0)
303 detach_conf(cptr, tmp->value.aconf);
308 * check_limit_and_attach - check client limits and attach I:line
310 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
311 * and dislallow more than 255 people here as well as in ipcheck.
312 * removed the old "ONE" scheme too.
313 * -- Isomer 2000-06-22
315 static enum AuthorizationCheckResult
316 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
321 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
322 number = *aconf->passwd-'0';
323 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
325 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
327 if (ip_registry_count(cptr->ip.s_addr) > number)
328 return ACR_TOO_MANY_FROM_IP;
329 return attach_conf(cptr, aconf);
333 * Find the first (best) I line to attach.
335 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
337 struct ConfItem* aconf;
340 static char uhost[HOSTLEN + USERLEN + 3];
341 static char fullname[HOSTLEN + 1];
342 struct hostent* hp = 0;
347 hp = cptr->dns_reply->hp;
349 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
350 if (aconf->status != CONF_CLIENT)
352 if (aconf->port && aconf->port != cptr->listener->port)
354 if (!aconf->host || !aconf->name)
357 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
358 ircd_strncpy(fullname, hname, HOSTLEN);
359 fullname[HOSTLEN] = '\0';
361 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
363 if (strchr(aconf->name, '@')) {
364 strcpy(uhost, cptr->username);
369 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
370 uhost[sizeof(uhost) - 1] = 0;
371 if (0 == match(aconf->name, uhost)) {
372 if (strchr(uhost, '@'))
373 cptr->flags |= FLAGS_DOID;
374 return check_limit_and_attach(cptr, aconf);
378 if (strchr(aconf->host, '@')) {
379 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
380 uhost[sizeof(uhost) - 2] = 0;
385 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
386 uhost[sizeof(uhost) - 1] = 0;
387 if (match(aconf->host, uhost))
389 if (strchr(uhost, '@'))
390 cptr->flags |= FLAGS_DOID;
392 return check_limit_and_attach(cptr, aconf);
394 return ACR_NO_AUTHORIZATION;
398 * detach_conf - Disassociate configuration from the client.
400 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
407 assert(0 < aconf->clients);
412 if ((*lp)->value.aconf == aconf) {
413 if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) &&
414 ConfLinks(aconf) > 0)
416 if (0 == --aconf->clients && IsIllegal(aconf))
429 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
433 for (lp = cptr->confs; lp; lp = lp->next)
434 if (lp->value.aconf == aconf)
443 * Associate a specific configuration entry to a *local*
444 * client (this is the one which used in accepting the
445 * connection). Note, that this automaticly changes the
446 * attachment if there was an old one...
448 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
452 if (is_attached(aconf, cptr))
453 return ACR_ALREADY_AUTHORIZED;
454 if (IsIllegal(aconf))
455 return ACR_NO_AUTHORIZATION;
456 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
457 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
458 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
460 lp->next = cptr->confs;
461 lp->value.aconf = aconf;
464 if (aconf->status & CONF_CLIENT_MASK)
469 const struct LocalConf* conf_get_local(void)
474 struct ConfItem* find_me(void)
476 struct ConfItem* aconf;
477 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
478 if (aconf->status & CONF_ME)
485 * attach_confs_byname
487 * Attach a CONF line to a client if the name passed matches that for
488 * the conf file (for non-C/N lines) or is an exact match (C/N lines
489 * only). The difference in behaviour is to stop C:*::* and N:*::*.
491 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
494 struct ConfItem* tmp;
495 struct ConfItem* first = NULL;
499 if (HOSTLEN < strlen(name))
502 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
503 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
504 assert(0 != tmp->name);
505 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
506 if (ACR_OK == attach_conf(cptr, tmp) && !first)
515 * Added for new access check meLazy
517 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
520 struct ConfItem* tmp;
521 struct ConfItem* first = 0;
524 if (HOSTLEN < strlen(host))
527 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
528 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
529 assert(0 != tmp->host);
530 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
531 if (ACR_OK == attach_conf(cptr, tmp) && !first)
540 * find a conf entry which matches the hostname and has the same name.
542 struct ConfItem* find_conf_exact(const char* name, const char* user,
543 const char* host, int statmask)
545 struct ConfItem *tmp;
546 char userhost[USERLEN + HOSTLEN + 3];
549 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
551 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
553 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
554 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
555 0 != ircd_strcmp(tmp->name, name))
558 * Accept if the *real* hostname (usually sockecthost)
559 * socket host) matches *either* host or name field
560 * of the configuration.
562 if (match(tmp->host, userhost))
564 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
565 if (tmp->clients < MaxLinks(tmp->confClass))
576 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
579 struct ConfItem* tmp;
582 if (HOSTLEN < strlen(name))
585 for (; lp; lp = lp->next) {
586 tmp = lp->value.aconf;
587 if (0 != (tmp->status & statmask)) {
588 assert(0 != tmp->name);
589 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
597 * Added for new access check meLazy
599 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
602 struct ConfItem* tmp = NULL;
605 if (HOSTLEN < strlen(host))
608 for (; lp; lp = lp->next) {
609 tmp = lp->value.aconf;
610 if (0 != (tmp->status & statmask)) {
611 assert(0 != tmp->host);
612 if (0 == match(tmp->host, host))
622 * Find a conf line using the IP# stored in it to search upon.
623 * Added 1/8/92 by Avalon.
625 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
628 struct ConfItem* tmp;
630 for (; lp; lp = lp->next) {
631 tmp = lp->value.aconf;
632 if (0 != (tmp->status & statmask)) {
633 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
643 * - looks for a match on all given fields.
645 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
648 struct ConfItem *bconf;
651 mask &= ~CONF_ILLEGAL;
653 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
654 if (!(bconf->status & mask) || (bconf->port != aconf->port))
657 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
658 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
660 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
663 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
664 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
666 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
668 && 0 != ircd_strcmp(bconf->passwd, "ONE")
670 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
673 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
674 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
676 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
685 * If conf line is a class definition, create a class entry
686 * for it and make the conf_line illegal and delete it.
688 void conf_add_class(const char* const* fields, int count)
692 add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
693 atoi(fields[4]), atoi(fields[5]));
696 void conf_add_listener(const char* const* fields, int count)
704 if (count < 5 || EmptyString(fields[4]))
707 if (!EmptyString(fields[3])) {
708 const char* x = fields[3];
709 if ('S' == ToUpper(*x))
712 if ('H' == ToUpper(*x))
715 /* port vhost mask */
716 add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
719 void conf_add_admin(const char* const* fields, int count)
722 * if you have one, it MUST have 3 lines
725 Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
726 ircd_log(L_CRIT, "Your A: line must have 4 fields!\n");
729 MyFree(localConf.location1);
730 DupString(localConf.location1, fields[1]);
732 MyFree(localConf.location2);
733 DupString(localConf.location2, fields[2]);
735 MyFree(localConf.contact);
736 DupString(localConf.contact, fields[3]);
739 void conf_add_motd(const char* const* fields, int count, struct MotdConf** list)
741 struct MotdConf* conf;
742 if (count < 3 || EmptyString(fields[1]) || EmptyString(fields[2]))
745 conf = (struct MotdConf*) MyMalloc(sizeof(struct MotdConf));
748 DupString(conf->hostmask, fields[1]);
749 collapse(conf->hostmask);
751 DupString(conf->path, fields[2]);
759 void conf_erase_motd_list(struct MotdConf** list)
762 struct MotdConf* next;
766 for (p = *list; p; p = next) {
775 const struct MotdConf* conf_get_motd_list(void)
781 * conf_add_crule - Create expression tree from connect rule and add it
784 void conf_add_crule(const char* const* fields, int count, int type)
786 struct CRuleNode* node;
789 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
792 if ((node = crule_parse(fields[3]))) {
793 struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
796 DupString(p->hostmask, fields[1]);
797 collapse(p->hostmask);
799 DupString(p->rule, fields[3]);
803 p->next = cruleConfList;
808 void conf_erase_crule_list(void)
810 struct CRuleConf* next;
811 struct CRuleConf* p = cruleConfList;
813 for ( ; p; p = next) {
815 crule_free(&p->node);
823 const struct CRuleConf* conf_get_crule_list(void)
825 return cruleConfList;
829 * read_configuration_file
831 * Read configuration file.
833 * returns 0, if file cannot be opened
837 #define MAXCONFLINKS 150
839 int read_configuration_file(void)
841 enum { MAX_FIELDS = 15 };
849 struct ConfItem *aconf = 0;
852 const char* field_vector[MAX_FIELDS + 1];
854 Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
855 if (0 == (file = fbopen(configfile, "r"))) {
859 while (fbgets(line, sizeof(line) - 1, file)) {
860 if ('#' == *line || IsSpace(*line))
863 if ((src = strchr(line, '\n')))
866 if (':' != line[1]) {
867 Debug((DEBUG_ERROR, "Bad config line: %s", line));
868 sendto_op_mask(SNO_OLDSNO,"Bad Config line");
873 * do escapes, quoting, comments, and field breakup in place
874 * in one pass with a poor mans state machine
876 field_vector[0] = line;
880 for (src = line, dest = line; *src; ) {
935 field_vector[field_count++] = dest;
936 if (field_count > MAX_FIELDS)
952 if (field_count < 2 || EmptyString(field_vector[0]))
960 switch (*field_vector[0]) {
961 case 'A': /* Name, e-mail address of administrator */
962 case 'a': /* of this server. CONF_ADMIN */
963 conf_add_admin(field_vector, field_count);
964 aconf->status = CONF_ILLEGAL;
966 case 'C': /* Server where I should try to connect */
967 case 'c': /* in case of lp failures */
969 aconf->status = CONF_SERVER;
972 case 'D': /* CONF_CRULEALL */
973 conf_add_crule(field_vector, field_count, CRULE_ALL);
974 aconf->status = CONF_ILLEGAL;
976 /* Connect rule - autos only */
977 case 'd': /* CONF_CRULEAUTO */
978 conf_add_crule(field_vector, field_count, CRULE_AUTO);
979 aconf->status = CONF_ILLEGAL;
981 case 'H': /* Hub server line */
983 aconf->status = CONF_HUB;
985 case 'I': /* Just plain normal irc client trying */
986 case 'i': /* to connect me */
987 aconf->status = CONF_CLIENT;
989 case 'K': /* Kill user line on irc.conf */
990 aconf->status = CONF_KILL;
992 case 'k': /* Kill user line based on IP in ircd.conf */
993 aconf->status = CONF_IPKILL;
995 /* Operator. Line should contain at least */
996 /* password and host where connection is */
997 case 'L': /* guaranteed leaf server */
999 aconf->status = CONF_LEAF;
1001 /* Me. Host field is name used for this host */
1002 /* and port number is the number of the port */
1005 aconf->status = CONF_ME;
1008 aconf->status = CONF_OPERATOR;
1010 /* Local Operator, (limited privs --SRB) */
1012 aconf->status = CONF_LOCOP;
1014 case 'P': /* listen port line */
1015 case 'p': /* CONF_LISTEN_PORT */
1016 conf_add_listener(field_vector, field_count);
1017 aconf->status = CONF_ILLEGAL;
1019 case 'T': /* print out different motd's */
1020 case 't': /* based on hostmask - CONF_TLINES */
1021 conf_add_motd(field_vector, field_count, &motdConfList);
1022 aconf->status = CONF_ILLEGAL;
1024 case 'U': /* Underworld server, allowed to hack modes */
1025 case 'u': /* *Every* server on the net must define the same !!! */
1026 aconf->status = CONF_UWORLD;
1029 case 'y': /* CONF_CLASS */
1030 conf_add_class(field_vector, field_count);
1031 aconf->status = CONF_ILLEGAL;
1034 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1035 sendto_op_mask(SNO_OLDSNO,"Unknown prefix in config file: %c", *field_vector[0]);
1036 aconf->status = CONF_ILLEGAL;
1039 if (IsIllegal(aconf))
1042 if (!EmptyString(field_vector[1]))
1043 DupString(aconf->host, field_vector[1]);
1045 if (!EmptyString(field_vector[2]))
1046 DupString(aconf->passwd, field_vector[2]);
1048 if (field_count > 3 && !EmptyString(field_vector[3]))
1049 DupString(aconf->name, field_vector[3]);
1051 if (field_count > 4 && !EmptyString(field_vector[4]))
1052 aconf->port = atoi(field_vector[4]);
1054 if (field_count > 5 && !EmptyString(field_vector[5])) {
1055 int n = atoi(field_vector[5]);
1056 if (CONF_ME == (aconf->status & CONF_ME))
1057 SetYXXServerName(&me, n); /* Our Numeric Nick */
1059 aconf->confClass = find_class(n);
1061 else if (CONF_ME == (aconf->status & CONF_ME)) {
1062 Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1063 "assigned to you by routing-com!\n"));
1064 ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1065 "assigned to you by routing-com!\n");
1069 * Associate each conf line with a class by using a pointer
1070 * to the correct class record. -avalon
1072 if (aconf->status & CONF_CLIENT_MASK) {
1073 if (aconf->confClass == 0)
1074 aconf->confClass = find_class(0);
1076 if (aconf->status & CONF_CLIENT) {
1077 struct ConfItem *bconf;
1079 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1081 bconf->status &= ~CONF_ILLEGAL;
1082 if (aconf->status == CONF_CLIENT) {
1084 * copy the password field in case it changed
1086 MyFree(bconf->passwd);
1087 bconf->passwd = aconf->passwd;
1090 ConfLinks(bconf) -= bconf->clients;
1091 bconf->confClass = aconf->confClass;
1092 if (bconf->confClass)
1093 ConfLinks(bconf) += bconf->clients;
1099 if (aconf->status & CONF_SERVER) {
1100 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1101 strchr(aconf->host, '?') || !aconf->name)
1104 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1105 if (!strchr(aconf->host, '@')) {
1107 int len = 3; /* *@\0 = 3 */
1109 len += strlen(aconf->host);
1110 newhost = (char*) MyMalloc(len);
1111 assert(0 != newhost);
1112 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1113 MyFree(aconf->host);
1114 aconf->host = newhost;
1117 if (aconf->status & CONF_SERVER) {
1118 if (EmptyString(aconf->passwd))
1120 lookup_confhost(aconf);
1123 * Own port and name cannot be changed after the startup.
1124 * (or could be allowed, but only if all links are closed first).
1125 * Configuration info does not override the name and port
1126 * if previously defined. Note, that "info"-field can be
1127 * changed by "/rehash".
1129 if (aconf->status == CONF_ME) {
1130 ircd_strncpy(me.info, aconf->name, REALLEN);
1133 if (aconf->status == CONF_IPKILL) {
1135 * Here we use the same kludge as in listener.c to parse
1136 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
1142 class=sscanf(aconf->host,"%d.%d.%d.%d/%d",
1143 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
1145 aconf->bits=class*8;
1150 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0], ad[1], ad[2], ad[3]);
1152 /* This ensures endian correctness */
1153 aconf->ipnum.s_addr = inet_addr(ipname);
1154 Debug((DEBUG_DEBUG,"IPkill: %s = %08x/%i (%08x)",ipname,
1155 aconf->ipnum.s_addr,aconf->bits,NETMASK(aconf->bits)));
1159 * Juped nicks are listed in the 'password' field of U:lines,
1160 * the list is comma separated and might be empty and/or contain
1161 * empty elements... the only limit is that it MUST be shorter
1162 * than 512 chars, or they will be cutted out :)
1164 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1165 addNickJupes(aconf->passwd);
1167 collapse(aconf->host);
1168 collapse(aconf->name);
1169 Debug((DEBUG_NOTICE,
1170 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1171 aconf->status, aconf->host, aconf->passwd,
1172 aconf->name, aconf->port, aconf->confClass));
1173 aconf->next = GlobalConfList;
1174 GlobalConfList = aconf;
1181 nextping = nextconnect = CurrentTime;
1188 * Actual REHASH service routine. Called with sig == 0 if it has been called
1189 * as a result of an operator issuing this command, else assume it has been
1190 * called as a result of the server receiving a HUP signal.
1192 int rehash(struct Client *cptr, int sig)
1194 struct ConfItem** tmp = &GlobalConfList;
1195 struct ConfItem* tmp2;
1196 struct ConfClass* cltmp;
1197 struct Client* acptr;
1198 struct MotdItem* temp;
1204 sendto_opmask_butone(0, SNO_OLDSNO,
1205 "Got signal SIGHUP, reloading ircd conf. file");
1207 while ((tmp2 = *tmp)) {
1208 if (tmp2->clients) {
1210 * Configuration entry is still in use by some
1211 * local clients, cannot delete it--mark it so
1212 * that it will be deleted when the last client
1215 if (!(tmp2->status & CONF_CLIENT)) {
1221 tmp2->status |= CONF_ILLEGAL;
1228 conf_erase_motd_list(&motdConfList);
1229 conf_erase_crule_list();
1232 * We don't delete the class table, rather mark all entries
1233 * for deletion. The table is cleaned up by check_class(). - avalon
1235 for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
1239 * delete the juped nicks list
1244 flush_resolver_cache();
1246 mark_listeners_closing();
1248 if (!read_configuration_file()) /* This calls check_class(), */
1249 check_class(); /* unless it fails */
1254 * Flush out deleted I and P lines although still in use.
1256 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1257 if (!(tmp2->status & CONF_ILLEGAL))
1267 for (i = 0; i <= HighestFd; i++) {
1268 if ((acptr = LocalClientArray[i])) {
1269 assert(!IsMe(acptr));
1270 if (IsServer(acptr)) {
1271 det_confs_butmask(acptr,
1272 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1273 attach_confs_byname(acptr, acptr->name,
1274 CONF_HUB | CONF_LEAF | CONF_UWORLD);
1276 /* Because admin's are getting so uppity about people managing to
1277 * get past K/G's etc, we'll "fix" the bug by actually explaining
1280 if ((found_g = find_kill(acptr))) {
1281 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1282 found_g == -2 ? "G-line active for %s%s" :
1283 "K-line active for %s%s",
1284 IsUnknown(acptr) ? "Unregistered Client ":"",
1285 get_client_name(acptr, HIDE_IP));
1286 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1287 "K-lined") == CPTR_KILLED)
1293 * free old motd structs
1305 /* reload motd files */
1307 rmotd = read_motd(RPATH);
1308 motd = read_motd(MPATH);
1315 * Read configuration file.
1317 * returns 0, if file cannot be opened
1323 if (read_configuration_file()) {
1325 * make sure we're sane to start if the config
1326 * file read didn't get everything we need.
1327 * XXX - should any of these abort the server?
1328 * TODO: add warning messages
1330 if (0 == localConf.location1)
1331 DupString(localConf.location1, "");
1332 if (0 == localConf.location2)
1333 DupString(localConf.location2, "");
1334 if (0 == localConf.contact)
1335 DupString(localConf.contact, "");
1344 * Read info from T:lines into TRecords which include the file
1345 * timestamp, the hostmask, and the contents of the motd file
1350 struct MotdConf* conf;
1351 struct TRecord *temp;
1352 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1353 struct MotdItem *amotd;
1355 /* Free the old trecords and the associated motd contents first */
1358 while (tdata->tmotd) {
1359 amotd = tdata->tmotd->next;
1360 MyFree(tdata->tmotd);
1361 tdata->tmotd = amotd;
1367 for (conf = motdConfList; conf; conf = conf->next) {
1368 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1371 temp->hostmask = conf->hostmask;
1372 temp->tmotd = read_motd(conf->path);
1373 temp->tmotd_tm = motd_tm;
1389 * 0: Client may continue to try and connect
1390 * -1: Client was K/k:'d - sideeffect: reason was sent.
1391 * -2: Client was G/g:'d - sideeffect: reason was sent.
1393 * Client may have been sent a reason why they are denied, as above.
1395 int find_kill(struct Client *cptr)
1399 struct ConfItem* tmp;
1400 struct Gline* agline = NULL;
1407 host = cptr->sockhost;
1408 name = cptr->user->username;
1410 assert(strlen(host) <= HOSTLEN);
1411 assert((name ? strlen(name) : 0) <= HOSTLEN);
1413 /* 2000-07-14: Rewrote this loop for massive speed increases.
1416 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1418 if ((tmp->status & CONF_KLINE) == 0)
1422 * What is this for? You can K: by port?!
1425 if (tmp->port && tmp->port != cptr->listener->port)
1428 if (!tmp->name || match(tmp->name, name) != 0)
1434 if (tmp->status != CONF_IPKILL) {
1435 if (match(tmp->host, host) == 0)
1438 else { /* k: by IP */
1439 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1440 cptr->ip.s_addr, tmp->ipnum.s_addr, tmp->bits,
1441 NETMASK(tmp->bits)));
1442 if ((cptr->ip.s_addr & NETMASK(tmp->bits)) == tmp->ipnum.s_addr)
1447 if (EmptyString(tmp->passwd))
1448 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1449 ":Connection from your host is refused on this server.");
1451 if (*tmp->passwd == '"') {
1452 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1453 strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1455 else if (*tmp->passwd == '!')
1456 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1458 #ifdef COMMENT_IS_FILE
1459 killcomment(cptr, cptr->name, tmp->passwd);
1461 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1467 /* find active glines */
1468 /* added a check against the user's IP address to find_gline() -Kev */
1469 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1470 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1471 GlineReason(agline));
1473 agline = NULL; /* if a gline was found, it was inactive */
1483 struct MotdItem* read_motd(const char* motdfile)
1485 FBFILE* file = NULL;
1486 struct MotdItem* temp;
1487 struct MotdItem* newmotd;
1488 struct MotdItem* last;
1493 if (NULL == (file = fbopen(motdfile, "r"))) {
1494 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1497 if (-1 == fbstat(&sb, file)) {
1501 newmotd = last = NULL;
1502 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1503 while (fbgets(line, sizeof(line) - 1, file)) {
1504 if ((tmp = (char *)strchr(line, '\n')))
1506 if ((tmp = (char *)strchr(line, '\r')))
1508 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1510 strcpy(temp->line, line);
1524 * Ordinary client access check. Look for conf lines which have the same
1525 * status as the flags passed.
1527 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1529 enum AuthorizationCheckResult acr = ACR_OK;
1533 if ((acr = attach_iline(cptr))) {
1534 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1535 cptr->name, cptr->sockhost));
1544 * Check access for a server given its name (passed in cptr struct).
1545 * Must check for all C/N lines which have a name which matches the
1546 * name given and a host which matches. A host alias which is the
1547 * same as the server name is also acceptable in the host field of a
1552 * -1 = Access denied
1555 int conf_check_server(struct Client *cptr)
1557 struct ConfItem* c_conf = NULL;
1560 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1561 cptr->name, cptr->sockhost));
1563 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1564 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1569 * We initiated this connection so the client should have a C and N
1570 * line already attached after passing through the connect_server()
1573 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1574 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1576 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1578 det_confs_butmask(cptr, 0);
1586 if (cptr->dns_reply) {
1588 struct hostent* hp = cptr->dns_reply->hp;
1589 const char* name = hp->h_name;
1591 * If we are missing a C or N line from above, search for
1592 * it under all known hostnames we have for this ip#.
1594 for (i = 0; name; name = hp->h_aliases[i++]) {
1595 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1596 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1601 for (i = 0; hp->h_addr_list[i]; i++) {
1602 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1609 * Check for C lines with the hostname portion the ip number
1610 * of the host the server runs on. This also checks the case where
1611 * there is a server connecting from 'localhost'.
1613 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1617 * Attach by IP# only if all other checks have failed.
1618 * It is quite possible to get here with the strange things that can
1619 * happen when using DNS in the way the irc server does. -avalon
1622 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1624 * detach all conf lines that got attached by attach_confs()
1626 det_confs_butmask(cptr, 0);
1628 * if no C or no N lines, then deny access
1631 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1632 cptr->name, cptr->username, cptr->sockhost));
1635 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1637 * attach the C and N lines to the client structure for later use.
1639 attach_conf(cptr, c_conf);
1640 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1642 if (INADDR_NONE == c_conf->ipnum.s_addr)
1643 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1645 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));