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;
81 * output the reason for being k lined from a file - Mmmm
83 * parv is the sender prefix
84 * filename is the file that is to be output to the K lined client
86 static void killcomment(struct Client *sptr, char *parv, char *filename)
94 if (NULL == (file = fbopen(filename, "r"))) {
95 send_reply(sptr, ERR_NOMOTD);
96 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
97 ":Connection from your host is refused on this server.");
101 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
102 while (fbgets(line, sizeof(line) - 1, file)) {
103 if ((tmp = strchr(line, '\n')))
105 if ((tmp = strchr(line, '\r')))
107 send_reply(sptr, RPL_MOTD, line);
109 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
110 ":Connection from your host is refused on this server.");
114 struct ConfItem* make_conf(void)
116 struct ConfItem* aconf;
118 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
123 aconf->status = CONF_ILLEGAL;
125 aconf->ipnum.s_addr = INADDR_NONE;
131 aconf->dns_pending = 0;
132 aconf->confClass = 0;
137 void delist_conf(struct ConfItem *aconf)
139 if (aconf == GlobalConfList)
140 GlobalConfList = GlobalConfList->next;
142 struct ConfItem *bconf;
144 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
146 bconf->next = aconf->next;
151 void free_conf(struct ConfItem *aconf)
153 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
154 aconf->host ? aconf->host : "*",
155 aconf->name ? aconf->name : "*",
157 if (aconf->dns_pending)
158 delete_resolver_queries(aconf);
161 memset(aconf->passwd, 0, strlen(aconf->passwd));
162 MyFree(aconf->passwd);
171 * conf_dns_callback - called when resolver query finishes
172 * if the query resulted in a successful search, hp will contain
173 * a non-null pointer, otherwise hp will be null.
174 * if successful save hp in the conf item it was called with
176 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
178 struct ConfItem* aconf = (struct ConfItem*) vptr;
179 aconf->dns_pending = 0;
181 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
185 * conf_dns_lookup - do a nameserver lookup of the conf host
186 * if the conf entry is currently doing a ns lookup do nothing, otherwise
187 * if the lookup returns a null pointer, set the conf dns_pending flag
189 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
191 struct DNSReply* dns_reply = 0;
192 if (!aconf->dns_pending) {
193 char buf[HOSTLEN + 1];
194 struct DNSQuery query;
196 query.callback = conf_dns_callback;
197 host_from_uh(buf, aconf->host, HOSTLEN);
200 if (0 == (dns_reply = gethost_byname(buf, &query)))
201 aconf->dns_pending = 1;
210 * Do (start) DNS lookups of all hostnames in the conf line and convert
211 * an IP addresses in a.b.c.d number for to IP#s.
213 static void lookup_confhost(struct ConfItem *aconf)
215 struct DNSReply* reply;
217 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
218 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
219 aconf->host, aconf->name));
223 * Do name lookup now on hostnames given and store the
224 * ip numbers in conf structure.
226 if (IsDigit(*aconf->host)) {
228 * rfc 1035 sez host names may not start with a digit
229 * XXX - this has changed code needs to be updated
231 aconf->ipnum.s_addr = inet_addr(aconf->host);
232 if (INADDR_NONE == aconf->ipnum.s_addr) {
233 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
234 aconf->host, aconf->name));
237 else if ((reply = conf_dns_lookup(aconf)))
238 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
242 * conf_find_server - find a server by name or hostname
243 * returns a server conf item pointer if found, 0 otherwise
245 * NOTE: at some point when we don't have to scan the entire
246 * list it may be cheaper to look for server names and host
247 * names in separate loops (original code did it that way)
249 struct ConfItem* conf_find_server(const char* name)
251 struct ConfItem* conf;
254 for (conf = GlobalConfList; conf; conf = conf->next) {
255 if (CONF_SERVER == conf->status) {
257 * Check first servernames, then try hostnames.
258 * XXX - match returns 0 if there _is_ a match... guess they
259 * haven't decided what true is yet
261 if (0 == match(name, conf->name))
269 * conf_eval_crule - evaluate connection rules
270 * returns the name of the rule triggered if found, 0 otherwise
272 * Evaluate connection rules... If no rules found, allow the
273 * connect. Otherwise stop with the first true rule (ie: rules
274 * are ored together. Oper connects are effected only by D
275 * lines (CRULEALL) not d lines (CRULEAUTO).
277 const char* conf_eval_crule(struct ConfItem* conf)
279 struct ConfItem* rule;
282 for (rule = GlobalConfList; rule; rule = rule->next) {
283 if ((CONF_CRULEALL == rule->status) && (0 == match(rule->host, conf->name))) {
284 if (crule_eval(rule->passwd))
292 * Remove all conf entries from the client except those which match
293 * the status field mask.
295 void det_confs_butmask(struct Client *cptr, int mask)
297 struct SLink *tmp, *tmp2;
299 for (tmp = cptr->confs; tmp; tmp = tmp2) {
301 if ((tmp->value.aconf->status & mask) == 0)
302 detach_conf(cptr, tmp->value.aconf);
307 * check_limit_and_attach - check client limits and attach I:line
309 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
310 * and dislallow more than 255 people here as well as in ipcheck.
311 * removed the old "ONE" scheme too.
312 * -- Isomer 2000-06-22
314 static enum AuthorizationCheckResult
315 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
320 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
321 number = *aconf->passwd-'0';
322 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
324 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
326 if (ip_registry_count(cptr->ip.s_addr) > number)
327 return ACR_TOO_MANY_FROM_IP;
328 return attach_conf(cptr, aconf);
332 * Find the first (best) I line to attach.
334 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
336 struct ConfItem* aconf;
339 static char uhost[HOSTLEN + USERLEN + 3];
340 static char fullname[HOSTLEN + 1];
341 struct hostent* hp = 0;
346 hp = cptr->dns_reply->hp;
348 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
349 if (aconf->status != CONF_CLIENT)
351 if (aconf->port && aconf->port != cptr->listener->port)
353 if (!aconf->host || !aconf->name)
356 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
357 ircd_strncpy(fullname, hname, HOSTLEN);
358 fullname[HOSTLEN] = '\0';
360 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
362 if (strchr(aconf->name, '@')) {
363 strcpy(uhost, cptr->username);
368 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
369 uhost[sizeof(uhost) - 1] = 0;
370 if (0 == match(aconf->name, uhost)) {
371 if (strchr(uhost, '@'))
372 cptr->flags |= FLAGS_DOID;
373 return check_limit_and_attach(cptr, aconf);
377 if (strchr(aconf->host, '@')) {
378 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
379 uhost[sizeof(uhost) - 2] = 0;
384 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
385 uhost[sizeof(uhost) - 1] = 0;
386 if (match(aconf->host, uhost))
388 if (strchr(uhost, '@'))
389 cptr->flags |= FLAGS_DOID;
391 return check_limit_and_attach(cptr, aconf);
393 return ACR_NO_AUTHORIZATION;
397 * detach_conf - Disassociate configuration from the client.
399 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
406 assert(0 < aconf->clients);
411 if ((*lp)->value.aconf == aconf) {
412 if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) &&
413 ConfLinks(aconf) > 0)
415 if (0 == --aconf->clients && IsIllegal(aconf))
428 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
432 for (lp = cptr->confs; lp; lp = lp->next)
433 if (lp->value.aconf == aconf)
442 * Associate a specific configuration entry to a *local*
443 * client (this is the one which used in accepting the
444 * connection). Note, that this automaticly changes the
445 * attachment if there was an old one...
447 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
451 if (is_attached(aconf, cptr))
452 return ACR_ALREADY_AUTHORIZED;
453 if (IsIllegal(aconf))
454 return ACR_NO_AUTHORIZATION;
455 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
456 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
457 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
459 lp->next = cptr->confs;
460 lp->value.aconf = aconf;
463 if (aconf->status & CONF_CLIENT_MASK)
468 const struct LocalConf* conf_get_local(void)
473 struct ConfItem* find_me(void)
475 struct ConfItem* aconf;
476 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
477 if (aconf->status & CONF_ME)
484 * attach_confs_byname
486 * Attach a CONF line to a client if the name passed matches that for
487 * the conf file (for non-C/N lines) or is an exact match (C/N lines
488 * only). The difference in behaviour is to stop C:*::* and N:*::*.
490 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
493 struct ConfItem* tmp;
494 struct ConfItem* first = NULL;
498 if (HOSTLEN < strlen(name))
501 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
502 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
503 assert(0 != tmp->name);
504 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
505 if (ACR_OK == attach_conf(cptr, tmp) && !first)
514 * Added for new access check meLazy
516 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
519 struct ConfItem* tmp;
520 struct ConfItem* first = 0;
523 if (HOSTLEN < strlen(host))
526 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
527 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
528 assert(0 != tmp->host);
529 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
530 if (ACR_OK == attach_conf(cptr, tmp) && !first)
539 * find a conf entry which matches the hostname and has the same name.
541 struct ConfItem* find_conf_exact(const char* name, const char* user,
542 const char* host, int statmask)
544 struct ConfItem *tmp;
545 char userhost[USERLEN + HOSTLEN + 3];
548 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
550 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
552 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
553 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
554 0 != ircd_strcmp(tmp->name, name))
557 * Accept if the *real* hostname (usually sockecthost)
558 * socket host) matches *either* host or name field
559 * of the configuration.
561 if (match(tmp->host, userhost))
563 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
564 if (tmp->clients < MaxLinks(tmp->confClass))
575 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
578 struct ConfItem* tmp;
581 if (HOSTLEN < strlen(name))
584 for (; lp; lp = lp->next) {
585 tmp = lp->value.aconf;
586 if (0 != (tmp->status & statmask)) {
587 assert(0 != tmp->name);
588 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
596 * Added for new access check meLazy
598 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
601 struct ConfItem* tmp = NULL;
604 if (HOSTLEN < strlen(host))
607 for (; lp; lp = lp->next) {
608 tmp = lp->value.aconf;
609 if (0 != (tmp->status & statmask)) {
610 assert(0 != tmp->host);
611 if (0 == match(tmp->host, host))
621 * Find a conf line using the IP# stored in it to search upon.
622 * Added 1/8/92 by Avalon.
624 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
627 struct ConfItem* tmp;
629 for (; lp; lp = lp->next) {
630 tmp = lp->value.aconf;
631 if (0 != (tmp->status & statmask)) {
632 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
642 * - looks for a match on all given fields.
644 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
647 struct ConfItem *bconf;
650 mask &= ~CONF_ILLEGAL;
652 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
653 if (!(bconf->status & mask) || (bconf->port != aconf->port))
656 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
657 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
659 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
662 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
663 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
665 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
667 && 0 != ircd_strcmp(bconf->passwd, "ONE")
669 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
672 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
673 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
675 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
684 * If conf line is a class definition, create a class entry
685 * for it and make the conf_line illegal and delete it.
687 void conf_add_class(const char* const* fields, int count)
691 add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
692 atoi(fields[4]), atoi(fields[5]));
695 void conf_add_listener(const char* const* fields, int count)
703 if (count < 5 || EmptyString(fields[4]))
706 if (!EmptyString(fields[3])) {
707 const char* x = fields[3];
708 if ('S' == ToUpper(*x))
711 if ('H' == ToUpper(*x))
714 /* port vhost mask */
715 add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
718 void conf_add_admin(const char* const* fields, int count)
721 * if you have one, it MUST have 3 lines
724 Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
725 ircd_log(L_CRIT, "Your A: line must have 4 fields!\n");
728 MyFree(localConf.location1);
729 DupString(localConf.location1, fields[1]);
731 MyFree(localConf.location2);
732 DupString(localConf.location2, fields[2]);
734 MyFree(localConf.contact);
735 DupString(localConf.contact, fields[3]);
738 void conf_add_motd(const char* const* fields, int count, struct MotdConf** list)
740 struct MotdConf* conf;
741 if (count < 3 || EmptyString(fields[1]) || EmptyString(fields[2]))
744 conf = (struct MotdConf*) MyMalloc(sizeof(struct MotdConf));
747 DupString(conf->hostmask, fields[1]);
748 collapse(conf->hostmask);
750 DupString(conf->path, fields[2]);
758 void erase_motd_conf_list(struct MotdConf** list)
761 struct MotdConf* next;
765 for (p = *list; p; p = next) {
774 const struct MotdConf* conf_get_motd_list(void)
780 * read_configuration_file
782 * Read configuration file.
784 * returns 0, if file cannot be opened
788 #define MAXCONFLINKS 150
790 int read_configuration_file(void)
792 enum { MAX_FIELDS = 15 };
800 struct ConfItem *aconf = 0;
803 const char* field_vector[MAX_FIELDS + 1];
805 Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
806 if (0 == (file = fbopen(configfile, "r"))) {
810 while (fbgets(line, sizeof(line) - 1, file)) {
811 if ('#' == *line || IsSpace(*line))
814 if ((src = strchr(line, '\n')))
817 if (':' != line[1]) {
818 Debug((DEBUG_ERROR, "Bad config line: %s", line));
819 sendto_op_mask(SNO_OLDSNO,"Bad Config line");
824 * do escapes, quoting, comments, and field breakup in place
825 * in one pass with a poor mans state machine
827 field_vector[0] = line;
831 for (src = line, dest = line; *src; ) {
883 field_vector[field_count++] = dest;
884 if (field_count > MAX_FIELDS)
900 if (field_count < 2 || EmptyString(field_vector[0]))
908 switch (*field_vector[0]) {
909 case 'A': /* Name, e-mail address of administrator */
910 case 'a': /* of this server. CONF_ADMIN */
911 conf_add_admin(field_vector, field_count);
912 aconf->status = CONF_ILLEGAL;
914 case 'C': /* Server where I should try to connect */
915 case 'c': /* in case of lp failures */
917 aconf->status = CONF_SERVER;
921 aconf->status = CONF_CRULEALL;
923 /* Connect rule - autos only */
925 aconf->status = CONF_CRULEAUTO;
927 case 'H': /* Hub server line */
929 aconf->status = CONF_HUB;
931 case 'I': /* Just plain normal irc client trying */
932 case 'i': /* to connect me */
933 aconf->status = CONF_CLIENT;
935 case 'K': /* Kill user line on irc.conf */
936 aconf->status = CONF_KILL;
938 case 'k': /* Kill user line based on IP in ircd.conf */
939 aconf->status = CONF_IPKILL;
941 /* Operator. Line should contain at least */
942 /* password and host where connection is */
943 case 'L': /* guaranteed leaf server */
945 aconf->status = CONF_LEAF;
947 /* Me. Host field is name used for this host */
948 /* and port number is the number of the port */
951 aconf->status = CONF_ME;
954 aconf->status = CONF_OPERATOR;
956 /* Local Operator, (limited privs --SRB) */
958 aconf->status = CONF_LOCOP;
960 case 'P': /* listen port line */
961 case 'p': /* CONF_LISTEN_PORT */
962 conf_add_listener(field_vector, field_count);
963 aconf->status = CONF_ILLEGAL;
965 case 'T': /* print out different motd's */
966 case 't': /* based on hostmask - CONF_TLINES */
967 conf_add_motd(field_vector, field_count, &motdConfList);
968 aconf->status = CONF_ILLEGAL;
970 case 'U': /* Underworld server, allowed to hack modes */
971 case 'u': /* *Every* server on the net must define the same !!! */
972 aconf->status = CONF_UWORLD;
975 case 'y': /* CONF_CLASS */
976 conf_add_class(field_vector, field_count);
977 aconf->status = CONF_ILLEGAL;
980 Debug((DEBUG_ERROR, "Error in config file: %s", line));
981 sendto_op_mask(SNO_OLDSNO,"Unknown prefix in config file: %c", *field_vector[0]);
982 aconf->status = CONF_ILLEGAL;
985 if (IsIllegal(aconf))
988 if (!EmptyString(field_vector[1]))
989 DupString(aconf->host, field_vector[1]);
991 if (!EmptyString(field_vector[2]))
992 DupString(aconf->passwd, field_vector[2]);
994 if (field_count > 3 && !EmptyString(field_vector[3]))
995 DupString(aconf->name, field_vector[3]);
997 if (field_count > 4 && !EmptyString(field_vector[4]))
998 aconf->port = atoi(field_vector[4]);
1000 if (field_count > 5 && !EmptyString(field_vector[5])) {
1001 int n = atoi(field_vector[5]);
1002 if (CONF_ME == (aconf->status & CONF_ME))
1003 SetYXXServerName(&me, n); /* Our Numeric Nick */
1005 aconf->confClass = find_class(n);
1007 else if (CONF_ME == (aconf->status & CONF_ME)) {
1008 Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1009 "assigned to you by routing-com!\n"));
1010 ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1011 "assigned to you by routing-com!\n");
1015 * Associate each conf line with a class by using a pointer
1016 * to the correct class record. -avalon
1018 if (aconf->status & CONF_CLIENT_MASK) {
1019 if (aconf->confClass == 0)
1020 aconf->confClass = find_class(0);
1022 if (aconf->status & CONF_CLIENT) {
1023 struct ConfItem *bconf;
1025 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1027 bconf->status &= ~CONF_ILLEGAL;
1028 if (aconf->status == CONF_CLIENT) {
1030 * copy the password field in case it changed
1032 MyFree(bconf->passwd);
1033 bconf->passwd = aconf->passwd;
1036 ConfLinks(bconf) -= bconf->clients;
1037 bconf->confClass = aconf->confClass;
1038 if (bconf->confClass)
1039 ConfLinks(bconf) += bconf->clients;
1045 if (aconf->status & CONF_SERVER) {
1046 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1047 strchr(aconf->host, '?') || !aconf->name)
1050 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1051 if (!strchr(aconf->host, '@')) {
1053 int len = 3; /* *@\0 = 3 */
1055 len += strlen(aconf->host);
1056 newhost = (char*) MyMalloc(len);
1057 assert(0 != newhost);
1058 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1059 MyFree(aconf->host);
1060 aconf->host = newhost;
1063 if (aconf->status & CONF_SERVER) {
1064 if (EmptyString(aconf->passwd))
1066 lookup_confhost(aconf);
1069 /* Create expression tree from connect rule...
1070 * If there's a parsing error, nuke the conf structure */
1071 if (aconf->status & (CONF_CRULEALL | CONF_CRULEAUTO)) {
1072 MyFree(aconf->passwd);
1073 if ((aconf->passwd = (char *)crule_parse(aconf->name)) == NULL) {
1081 * Own port and name cannot be changed after the startup.
1082 * (or could be allowed, but only if all links are closed first).
1083 * Configuration info does not override the name and port
1084 * if previously defined. Note, that "info"-field can be
1085 * changed by "/rehash".
1087 if (aconf->status == CONF_ME) {
1088 ircd_strncpy(me.info, aconf->name, REALLEN);
1091 if (aconf->status == CONF_IPKILL) {
1093 * Here we use the same kludge as in listener.c to parse
1094 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
1100 class=sscanf(aconf->host,"%d.%d.%d.%d/%d",
1101 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
1103 aconf->bits=class*8;
1108 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0], ad[1], ad[2], ad[3]);
1110 /* This ensures endian correctness */
1111 aconf->ipnum.s_addr = inet_addr(ipname);
1112 Debug((DEBUG_DEBUG,"IPkill: %s = %08x/%i (%08x)",ipname,
1113 aconf->ipnum.s_addr,aconf->bits,NETMASK(aconf->bits)));
1117 * Juped nicks are listed in the 'password' field of U:lines,
1118 * the list is comma separated and might be empty and/or contain
1119 * empty elements... the only limit is that it MUST be shorter
1120 * than 512 chars, or they will be cutted out :)
1122 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1123 addNickJupes(aconf->passwd);
1125 collapse(aconf->host);
1126 collapse(aconf->name);
1127 Debug((DEBUG_NOTICE,
1128 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1129 aconf->status, aconf->host, aconf->passwd,
1130 aconf->name, aconf->port, aconf->confClass));
1131 aconf->next = GlobalConfList;
1132 GlobalConfList = aconf;
1139 nextping = nextconnect = CurrentTime;
1146 * Actual REHASH service routine. Called with sig == 0 if it has been called
1147 * as a result of an operator issuing this command, else assume it has been
1148 * called as a result of the server receiving a HUP signal.
1150 int rehash(struct Client *cptr, int sig)
1152 struct ConfItem** tmp = &GlobalConfList;
1153 struct ConfItem* tmp2;
1154 struct ConfClass* cltmp;
1155 struct Client* acptr;
1156 struct MotdItem* temp;
1162 sendto_opmask_butone(0, SNO_OLDSNO,
1163 "Got signal SIGHUP, reloading ircd conf. file");
1165 while ((tmp2 = *tmp)) {
1166 if (tmp2->clients) {
1168 * Configuration entry is still in use by some
1169 * local clients, cannot delete it--mark it so
1170 * that it will be deleted when the last client
1173 if (!(tmp2->status & CONF_CLIENT)) {
1179 tmp2->status |= CONF_ILLEGAL;
1183 /* free expression trees of connect rules */
1184 if ((tmp2->status & (CONF_CRULEALL | CONF_CRULEAUTO)) &&
1185 (tmp2->passwd != NULL))
1186 crule_free(&(tmp2->passwd));
1190 erase_motd_conf_list(&motdConfList);
1192 * We don't delete the class table, rather mark all entries
1193 * for deletion. The table is cleaned up by check_class(). - avalon
1195 for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
1199 * delete the juped nicks list
1204 flush_resolver_cache();
1206 mark_listeners_closing();
1208 if (!read_configuration_file()) /* This calls check_class(), */
1209 check_class(); /* unless it fails */
1212 * make sure that the server listener is re-added so it doesn't get
1218 * Flush out deleted I and P lines although still in use.
1220 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1221 if (!(tmp2->status & CONF_ILLEGAL))
1231 for (i = 0; i <= HighestFd; i++) {
1232 if ((acptr = LocalClientArray[i])) {
1233 assert(!IsMe(acptr));
1234 if (IsServer(acptr)) {
1235 det_confs_butmask(acptr,
1236 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1237 attach_confs_byname(acptr, acptr->name,
1238 CONF_HUB | CONF_LEAF | CONF_UWORLD);
1240 /* Because admin's are getting so uppity about people managing to
1241 * get past K/G's etc, we'll "fix" the bug by actually explaining
1244 if ((found_g = find_kill(acptr))) {
1245 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1246 found_g == -2 ? "G-line active for %s%s" :
1247 "K-line active for %s%s",
1248 IsUnknown(acptr) ? "Unregistered Client ":"",
1249 get_client_name(acptr, HIDE_IP));
1250 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1251 "K-lined") == CPTR_KILLED)
1257 * free old motd structs
1269 /* reload motd files */
1271 rmotd = read_motd(RPATH);
1272 motd = read_motd(MPATH);
1279 * Read configuration file.
1281 * returns 0, if file cannot be opened
1287 if (read_configuration_file()) {
1289 * make sure we're sane to start if the config
1290 * file read didn't get everything we need.
1291 * XXX - should any of these abort the server?
1292 * TODO: add warning messages
1294 if (0 == localConf.location1)
1295 DupString(localConf.location1, "");
1296 if (0 == localConf.location2)
1297 DupString(localConf.location2, "");
1298 if (0 == localConf.contact)
1299 DupString(localConf.contact, "");
1308 * Read info from T:lines into TRecords which include the file
1309 * timestamp, the hostmask, and the contents of the motd file
1314 struct MotdConf* conf;
1315 struct TRecord *temp;
1316 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1317 struct MotdItem *amotd;
1319 /* Free the old trecords and the associated motd contents first */
1322 while (tdata->tmotd) {
1323 amotd = tdata->tmotd->next;
1324 MyFree(tdata->tmotd);
1325 tdata->tmotd = amotd;
1331 for (conf = motdConfList; conf; conf = conf->next) {
1332 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1335 temp->hostmask = conf->hostmask;
1336 temp->tmotd = read_motd(conf->path);
1337 temp->tmotd_tm = motd_tm;
1353 * 0: Client may continue to try and connect
1354 * -1: Client was K/k:'d - sideeffect: reason was sent.
1355 * -2: Client was G/g:'d - sideeffect: reason was sent.
1357 * Client may have been sent a reason why they are denied, as above.
1359 int find_kill(struct Client *cptr)
1363 struct ConfItem* tmp;
1364 struct Gline* agline = NULL;
1371 host = cptr->sockhost;
1372 name = cptr->user->username;
1374 assert(strlen(host) <= HOSTLEN);
1375 assert((name ? strlen(name) : 0) <= HOSTLEN);
1377 /* 2000-07-14: Rewrote this loop for massive speed increases.
1380 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1382 if ((tmp->status & CONF_KLINE) == 0)
1386 * What is this for? You can K: by port?!
1389 if (tmp->port && tmp->port != cptr->listener->port)
1392 if (!tmp->name || match(tmp->name, name) != 0)
1398 if (tmp->status != CONF_IPKILL) {
1399 if (match(tmp->host, host) == 0)
1402 else { /* k: by IP */
1403 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1404 cptr->ip.s_addr, tmp->ipnum.s_addr, tmp->bits,
1405 NETMASK(tmp->bits)));
1406 if ((cptr->ip.s_addr & NETMASK(tmp->bits)) == tmp->ipnum.s_addr)
1411 if (EmptyString(tmp->passwd))
1412 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1413 ":Connection from your host is refused on this server.");
1415 if (*tmp->passwd == '"') {
1416 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1417 strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1419 else if (*tmp->passwd == '!')
1420 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1422 #ifdef COMMENT_IS_FILE
1423 killcomment(cptr, cptr->name, tmp->passwd);
1425 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1431 /* find active glines */
1432 /* added a check against the user's IP address to find_gline() -Kev */
1433 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1434 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1435 GlineReason(agline));
1437 agline = NULL; /* if a gline was found, it was inactive */
1447 struct MotdItem* read_motd(const char* motdfile)
1449 FBFILE* file = NULL;
1450 struct MotdItem* temp;
1451 struct MotdItem* newmotd;
1452 struct MotdItem* last;
1457 if (NULL == (file = fbopen(motdfile, "r"))) {
1458 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1461 if (-1 == fbstat(&sb, file)) {
1465 newmotd = last = NULL;
1466 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1467 while (fbgets(line, sizeof(line) - 1, file)) {
1468 if ((tmp = (char *)strchr(line, '\n')))
1470 if ((tmp = (char *)strchr(line, '\r')))
1472 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1474 strcpy(temp->line, line);
1488 * Ordinary client access check. Look for conf lines which have the same
1489 * status as the flags passed.
1491 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1493 enum AuthorizationCheckResult acr = ACR_OK;
1497 if ((acr = attach_iline(cptr))) {
1498 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1499 cptr->name, cptr->sockhost));
1508 * Check access for a server given its name (passed in cptr struct).
1509 * Must check for all C/N lines which have a name which matches the
1510 * name given and a host which matches. A host alias which is the
1511 * same as the server name is also acceptable in the host field of a
1516 * -1 = Access denied
1519 int conf_check_server(struct Client *cptr)
1521 struct ConfItem* c_conf = NULL;
1524 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1525 cptr->name, cptr->sockhost));
1527 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1528 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1533 * We initiated this connection so the client should have a C and N
1534 * line already attached after passing through the connect_server()
1537 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1538 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1540 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1542 det_confs_butmask(cptr, 0);
1550 if (cptr->dns_reply) {
1552 struct hostent* hp = cptr->dns_reply->hp;
1553 const char* name = hp->h_name;
1555 * If we are missing a C or N line from above, search for
1556 * it under all known hostnames we have for this ip#.
1558 for (i = 0; name; name = hp->h_aliases[i++]) {
1559 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1560 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1565 for (i = 0; hp->h_addr_list[i]; i++) {
1566 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1573 * Check for C lines with the hostname portion the ip number
1574 * of the host the server runs on. This also checks the case where
1575 * there is a server connecting from 'localhost'.
1577 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1581 * Attach by IP# only if all other checks have failed.
1582 * It is quite possible to get here with the strange things that can
1583 * happen when using DNS in the way the irc server does. -avalon
1586 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1588 * detach all conf lines that got attached by attach_confs()
1590 det_confs_butmask(cptr, 0);
1592 * if no C or no N lines, then deny access
1595 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1596 cptr->name, cptr->username, cptr->sockhost));
1599 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1601 * attach the C and N lines to the client structure for later use.
1603 attach_conf(cptr, c_conf);
1604 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1606 if (INADDR_NONE == c_conf->ipnum.s_addr)
1607 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1609 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));