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;
80 static struct ServerConf* serverConfList;
83 * output the reason for being k lined from a file - Mmmm
85 * parv is the sender prefix
86 * filename is the file that is to be output to the K lined client
88 static void killcomment(struct Client *sptr, char *parv, char *filename)
96 if (NULL == (file = fbopen(filename, "r"))) {
97 send_reply(sptr, ERR_NOMOTD);
98 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
99 ":Connection from your host is refused on this server.");
103 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
104 while (fbgets(line, sizeof(line) - 1, file)) {
105 if ((tmp = strchr(line, '\n')))
107 if ((tmp = strchr(line, '\r')))
109 send_reply(sptr, RPL_MOTD, line);
111 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
112 ":Connection from your host is refused on this server.");
116 struct ConfItem* make_conf(void)
118 struct ConfItem* aconf;
120 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
125 aconf->status = CONF_ILLEGAL;
127 aconf->ipnum.s_addr = INADDR_NONE;
133 aconf->dns_pending = 0;
134 aconf->confClass = 0;
139 void delist_conf(struct ConfItem *aconf)
141 if (aconf == GlobalConfList)
142 GlobalConfList = GlobalConfList->next;
144 struct ConfItem *bconf;
146 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
148 bconf->next = aconf->next;
153 void free_conf(struct ConfItem *aconf)
155 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
156 aconf->host ? aconf->host : "*",
157 aconf->name ? aconf->name : "*",
159 if (aconf->dns_pending)
160 delete_resolver_queries(aconf);
163 memset(aconf->passwd, 0, strlen(aconf->passwd));
164 MyFree(aconf->passwd);
173 * conf_dns_callback - called when resolver query finishes
174 * if the query resulted in a successful search, hp will contain
175 * a non-null pointer, otherwise hp will be null.
176 * if successful save hp in the conf item it was called with
178 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
180 struct ConfItem* aconf = (struct ConfItem*) vptr;
181 aconf->dns_pending = 0;
183 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
187 * conf_dns_lookup - do a nameserver lookup of the conf host
188 * if the conf entry is currently doing a ns lookup do nothing, otherwise
189 * if the lookup returns a null pointer, set the conf dns_pending flag
191 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
193 struct DNSReply* dns_reply = 0;
194 if (!aconf->dns_pending) {
195 char buf[HOSTLEN + 1];
196 struct DNSQuery query;
198 query.callback = conf_dns_callback;
199 host_from_uh(buf, aconf->host, HOSTLEN);
202 if (0 == (dns_reply = gethost_byname(buf, &query)))
203 aconf->dns_pending = 1;
212 * Do (start) DNS lookups of all hostnames in the conf line and convert
213 * an IP addresses in a.b.c.d number for to IP#s.
215 static void lookup_confhost(struct ConfItem *aconf)
217 struct DNSReply* reply;
219 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
220 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
221 aconf->host, aconf->name));
225 * Do name lookup now on hostnames given and store the
226 * ip numbers in conf structure.
228 if (IsDigit(*aconf->host)) {
230 * rfc 1035 sez host names may not start with a digit
231 * XXX - this has changed code needs to be updated
233 aconf->ipnum.s_addr = inet_addr(aconf->host);
234 if (INADDR_NONE == aconf->ipnum.s_addr) {
235 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
236 aconf->host, aconf->name));
239 else if ((reply = conf_dns_lookup(aconf)))
240 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
244 * conf_find_server - find a server by name or hostname
245 * returns a server conf item pointer if found, 0 otherwise
247 * NOTE: at some point when we don't have to scan the entire
248 * list it may be cheaper to look for server names and host
249 * names in separate loops (original code did it that way)
251 struct ConfItem* conf_find_server(const char* name)
253 struct ConfItem* conf;
256 for (conf = GlobalConfList; conf; conf = conf->next) {
257 if (CONF_SERVER == conf->status) {
259 * Check first servernames, then try hostnames.
260 * XXX - match returns 0 if there _is_ a match... guess they
261 * haven't decided what true is yet
263 if (0 == match(name, conf->name))
271 * conf_eval_crule - evaluate connection rules
272 * returns the name of the rule triggered if found, 0 otherwise
274 * Evaluate connection rules... If no rules found, allow the
275 * connect. Otherwise stop with the first true rule (ie: rules
276 * are ored together. Oper connects are effected only by D
277 * lines (CRULE_ALL) not d lines (CRULE_AUTO).
279 const char* conf_eval_crule(const char* name, int mask)
281 struct CRuleConf* p = cruleConfList;
284 for ( ; p; p = p->next) {
285 if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
286 if (crule_eval(p->node))
294 * Remove all conf entries from the client except those which match
295 * the status field mask.
297 void det_confs_butmask(struct Client *cptr, int mask)
299 struct SLink *tmp, *tmp2;
301 for (tmp = cptr->confs; tmp; tmp = tmp2) {
303 if ((tmp->value.aconf->status & mask) == 0)
304 detach_conf(cptr, tmp->value.aconf);
309 * check_limit_and_attach - check client limits and attach I:line
311 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
312 * and dislallow more than 255 people here as well as in ipcheck.
313 * removed the old "ONE" scheme too.
314 * -- Isomer 2000-06-22
316 static enum AuthorizationCheckResult
317 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
322 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
323 number = *aconf->passwd-'0';
324 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
326 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
328 if (IPcheck_nr(cptr) > number)
329 return ACR_TOO_MANY_FROM_IP;
330 return attach_conf(cptr, aconf);
334 * Find the first (best) I line to attach.
336 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
338 struct ConfItem* aconf;
341 static char uhost[HOSTLEN + USERLEN + 3];
342 static char fullname[HOSTLEN + 1];
343 struct hostent* hp = 0;
348 hp = cptr->dns_reply->hp;
350 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
351 if (aconf->status != CONF_CLIENT)
353 if (aconf->port && aconf->port != cptr->listener->port)
355 if (!aconf->host || !aconf->name)
358 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
359 ircd_strncpy(fullname, hname, HOSTLEN);
360 fullname[HOSTLEN] = '\0';
362 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
364 if (strchr(aconf->name, '@')) {
365 strcpy(uhost, cptr->username);
370 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
371 uhost[sizeof(uhost) - 1] = 0;
372 if (0 == match(aconf->name, uhost)) {
373 if (strchr(uhost, '@'))
374 cptr->flags |= FLAGS_DOID;
375 return check_limit_and_attach(cptr, aconf);
379 if (strchr(aconf->host, '@')) {
380 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
381 uhost[sizeof(uhost) - 2] = 0;
386 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
387 uhost[sizeof(uhost) - 1] = 0;
388 if (match(aconf->host, uhost))
390 if (strchr(uhost, '@'))
391 cptr->flags |= FLAGS_DOID;
393 return check_limit_and_attach(cptr, aconf);
395 return ACR_NO_AUTHORIZATION;
399 * detach_conf - Disassociate configuration from the client.
401 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
408 assert(0 < aconf->clients);
413 if ((*lp)->value.aconf == aconf) {
414 if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) &&
415 ConfLinks(aconf) > 0)
417 if (0 == --aconf->clients && IsIllegal(aconf))
430 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
434 for (lp = cptr->confs; lp; lp = lp->next)
435 if (lp->value.aconf == aconf)
444 * Associate a specific configuration entry to a *local*
445 * client (this is the one which used in accepting the
446 * connection). Note, that this automaticly changes the
447 * attachment if there was an old one...
449 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
453 if (is_attached(aconf, cptr))
454 return ACR_ALREADY_AUTHORIZED;
455 if (IsIllegal(aconf))
456 return ACR_NO_AUTHORIZATION;
457 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
458 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
459 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
461 lp->next = cptr->confs;
462 lp->value.aconf = aconf;
465 if (aconf->status & CONF_CLIENT_MASK)
470 const struct LocalConf* conf_get_local(void)
476 * attach_confs_byname
478 * Attach a CONF line to a client if the name passed matches that for
479 * the conf file (for non-C/N lines) or is an exact match (C/N lines
480 * only). The difference in behaviour is to stop C:*::* and N:*::*.
482 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
485 struct ConfItem* tmp;
486 struct ConfItem* first = NULL;
490 if (HOSTLEN < strlen(name))
493 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
494 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
495 assert(0 != tmp->name);
496 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
497 if (ACR_OK == attach_conf(cptr, tmp) && !first)
506 * Added for new access check meLazy
508 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
511 struct ConfItem* tmp;
512 struct ConfItem* first = 0;
515 if (HOSTLEN < strlen(host))
518 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
519 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
520 assert(0 != tmp->host);
521 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
522 if (ACR_OK == attach_conf(cptr, tmp) && !first)
531 * find a conf entry which matches the hostname and has the same name.
533 struct ConfItem* find_conf_exact(const char* name, const char* user,
534 const char* host, int statmask)
536 struct ConfItem *tmp;
537 char userhost[USERLEN + HOSTLEN + 3];
540 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
542 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
544 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
545 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
546 0 != ircd_strcmp(tmp->name, name))
549 * Accept if the *real* hostname (usually sockecthost)
550 * socket host) matches *either* host or name field
551 * of the configuration.
553 if (match(tmp->host, userhost))
555 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
556 if (tmp->clients < MaxLinks(tmp->confClass))
567 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
570 struct ConfItem* tmp;
573 if (HOSTLEN < strlen(name))
576 for (; lp; lp = lp->next) {
577 tmp = lp->value.aconf;
578 if (0 != (tmp->status & statmask)) {
579 assert(0 != tmp->name);
580 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
588 * Added for new access check meLazy
590 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
593 struct ConfItem* tmp = NULL;
596 if (HOSTLEN < strlen(host))
599 for (; lp; lp = lp->next) {
600 tmp = lp->value.aconf;
601 if (0 != (tmp->status & statmask)) {
602 assert(0 != tmp->host);
603 if (0 == match(tmp->host, host))
613 * Find a conf line using the IP# stored in it to search upon.
614 * Added 1/8/92 by Avalon.
616 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
619 struct ConfItem* tmp;
621 for (; lp; lp = lp->next) {
622 tmp = lp->value.aconf;
623 if (0 != (tmp->status & statmask)) {
624 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
634 * - looks for a match on all given fields.
636 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
639 struct ConfItem *bconf;
642 mask &= ~CONF_ILLEGAL;
644 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
645 if (!(bconf->status & mask) || (bconf->port != aconf->port))
648 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
649 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
651 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
654 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
655 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
657 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
659 && 0 != ircd_strcmp(bconf->passwd, "ONE")
661 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
664 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
665 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
667 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
676 * If conf line is a class definition, create a class entry
677 * for it and make the conf_line illegal and delete it.
679 void conf_add_class(const char* const* fields, int count)
683 add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
684 atoi(fields[4]), atoi(fields[5]));
687 void conf_add_listener(const char* const* fields, int count)
695 if (count < 5 || EmptyString(fields[4]))
698 if (!EmptyString(fields[3])) {
699 const char* x = fields[3];
700 if ('S' == ToUpper(*x))
703 if ('H' == ToUpper(*x))
706 /* port vhost mask */
707 add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
710 void conf_add_local(const char* const* fields, int count)
712 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[5])) {
713 ircd_log(L_CRIT, "Your M: line must have 6 fields!\n");
717 * these two can only be set the first time
719 if (0 == localConf.name) {
720 if (string_is_hostname(fields[1]))
721 DupString(localConf.name, fields[1]);
723 if (0 == localConf.numeric) {
724 localConf.numeric = atoi(fields[5]);
725 if (0 == localConf.numeric)
726 ircd_log(L_WARNING, "Your M: line must have a Numeric value greater than 0\n");
729 * these two can be changed while the server is running
731 if (string_is_address(fields[2])) {
732 if (INADDR_NONE == (localConf.vhost_address.s_addr = inet_addr(fields[2])))
733 localConf.vhost_address.s_addr = INADDR_ANY;
735 MyFree(localConf.description);
736 DupString(localConf.description, fields[3]);
738 * XXX - shouldn't be setting these directly here
740 ircd_strncpy(me.info, fields[3], REALLEN);
741 set_virtual_host(localConf.vhost_address);
744 void conf_add_admin(const char* const* fields, int count)
747 * if you have one, it MUST have 3 lines
750 ircd_log(L_CRIT, "Your A: line must have 4 fields!\n");
753 MyFree(localConf.location1);
754 DupString(localConf.location1, fields[1]);
756 MyFree(localConf.location2);
757 DupString(localConf.location2, fields[2]);
759 MyFree(localConf.contact);
760 DupString(localConf.contact, fields[3]);
763 void conf_add_motd(const char* const* fields, int count, struct MotdConf** list)
765 struct MotdConf* conf;
766 if (count < 3 || EmptyString(fields[1]) || EmptyString(fields[2]))
769 conf = (struct MotdConf*) MyMalloc(sizeof(struct MotdConf));
772 DupString(conf->hostmask, fields[1]);
773 collapse(conf->hostmask);
775 DupString(conf->path, fields[2]);
783 void conf_erase_motd_list(struct MotdConf** list)
786 struct MotdConf* next;
790 for (p = *list; p; p = next) {
799 const struct MotdConf* conf_get_motd_list(void)
805 * conf_add_crule - Create expression tree from connect rule and add it
808 void conf_add_crule(const char* const* fields, int count, int type)
810 struct CRuleNode* node;
813 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
816 if ((node = crule_parse(fields[3]))) {
817 struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
820 DupString(p->hostmask, fields[1]);
821 collapse(p->hostmask);
823 DupString(p->rule, fields[3]);
827 p->next = cruleConfList;
832 void conf_erase_crule_list(void)
834 struct CRuleConf* next;
835 struct CRuleConf* p = cruleConfList;
837 for ( ; p; p = next) {
839 crule_free(&p->node);
847 const struct CRuleConf* conf_get_crule_list(void)
849 return cruleConfList;
852 void conf_add_server(const char* const* fields, int count)
854 struct ServerConf* server;
858 * missing host, password, or alias?
860 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
865 if (string_is_hostname(fields[1]))
866 addr.s_addr = INADDR_NONE;
867 else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
870 server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
872 DupString(server->hostname, fields[1]);
873 DupString(server->passwd, fields[2]);
874 DupString(server->alias, fields[3]);
875 server->address.s_addr = addr.s_addr;
876 server->port = atoi(fields[4]);
877 server->dns_pending = 0;
878 server->connected = 0;
880 server->confClass = find_class(atoi(fields[5]));
882 server->next = serverConfList;
883 serverConfList = server;
885 // if (INADDR_NONE == server->address.s_addr)
886 // lookup_confhost(server);
891 * read_configuration_file
893 * Read configuration file.
895 * returns 0, if file cannot be opened
899 #define MAXCONFLINKS 150
901 int read_configuration_file(void)
903 enum { MAX_FIELDS = 15 };
911 struct ConfItem *aconf = 0;
914 const char* field_vector[MAX_FIELDS + 1];
916 Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
917 if (0 == (file = fbopen(configfile, "r"))) {
921 while (fbgets(line, sizeof(line) - 1, file)) {
922 if ('#' == *line || IsSpace(*line))
925 if ((src = strchr(line, '\n')))
928 if (':' != line[1]) {
929 Debug((DEBUG_ERROR, "Bad config line: %s", line));
930 sendto_op_mask(SNO_OLDSNO,"Bad Config line");
935 * do escapes, quoting, comments, and field breakup in place
936 * in one pass with a poor mans state machine
938 field_vector[0] = line;
942 for (src = line, dest = line; *src; ) {
997 field_vector[field_count++] = dest;
998 if (field_count > MAX_FIELDS)
1014 if (field_count < 2 || EmptyString(field_vector[0]))
1020 aconf = make_conf();
1022 switch (*field_vector[0]) {
1023 case 'A': /* Name, e-mail address of administrator */
1024 case 'a': /* of this server. CONF_ADMIN */
1025 conf_add_admin(field_vector, field_count);
1026 aconf->status = CONF_ILLEGAL;
1028 case 'C': /* Server where I should try to connect */
1029 case 'c': /* in case of lp failures */
1031 aconf->status = CONF_SERVER;
1034 case 'D': /* CONF_CRULEALL */
1035 conf_add_crule(field_vector, field_count, CRULE_ALL);
1036 aconf->status = CONF_ILLEGAL;
1038 /* Connect rule - autos only */
1039 case 'd': /* CONF_CRULEAUTO */
1040 conf_add_crule(field_vector, field_count, CRULE_AUTO);
1041 aconf->status = CONF_ILLEGAL;
1043 case 'H': /* Hub server line */
1045 aconf->status = CONF_HUB;
1047 case 'I': /* Just plain normal irc client trying */
1048 case 'i': /* to connect me */
1049 aconf->status = CONF_CLIENT;
1051 case 'K': /* Kill user line on irc.conf */
1052 aconf->status = CONF_KILL;
1054 case 'k': /* Kill user line based on IP in ircd.conf */
1055 aconf->status = CONF_IPKILL;
1057 /* Operator. Line should contain at least */
1058 /* password and host where connection is */
1059 case 'L': /* guaranteed leaf server */
1061 aconf->status = CONF_LEAF;
1063 /* Me. Host field is name used for this host */
1064 /* and port number is the number of the port */
1066 case 'm': /* CONF_ME */
1067 conf_add_local(field_vector, field_count);
1068 aconf->status = CONF_ILLEGAL;
1071 aconf->status = CONF_OPERATOR;
1073 /* Local Operator, (limited privs --SRB) */
1075 aconf->status = CONF_LOCOP;
1077 case 'P': /* listen port line */
1078 case 'p': /* CONF_LISTEN_PORT */
1079 conf_add_listener(field_vector, field_count);
1080 aconf->status = CONF_ILLEGAL;
1082 case 'T': /* print out different motd's */
1083 case 't': /* based on hostmask - CONF_TLINES */
1084 conf_add_motd(field_vector, field_count, &motdConfList);
1085 aconf->status = CONF_ILLEGAL;
1087 case 'U': /* Underworld server, allowed to hack modes */
1088 case 'u': /* *Every* server on the net must define the same !!! */
1089 aconf->status = CONF_UWORLD;
1092 case 'y': /* CONF_CLASS */
1093 conf_add_class(field_vector, field_count);
1094 aconf->status = CONF_ILLEGAL;
1097 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1098 sendto_op_mask(SNO_OLDSNO,"Unknown prefix in config file: %c", *field_vector[0]);
1099 aconf->status = CONF_ILLEGAL;
1102 if (IsIllegal(aconf))
1105 if (!EmptyString(field_vector[1]))
1106 DupString(aconf->host, field_vector[1]);
1108 if (!EmptyString(field_vector[2]))
1109 DupString(aconf->passwd, field_vector[2]);
1111 if (field_count > 3 && !EmptyString(field_vector[3]))
1112 DupString(aconf->name, field_vector[3]);
1114 if (field_count > 4 && !EmptyString(field_vector[4]))
1115 aconf->port = atoi(field_vector[4]);
1117 if (field_count > 5 && !EmptyString(field_vector[5]))
1118 aconf->confClass = find_class(atoi(field_vector[5]));
1121 * Associate each conf line with a class by using a pointer
1122 * to the correct class record. -avalon
1124 if (aconf->status & CONF_CLIENT_MASK) {
1125 if (aconf->confClass == 0)
1126 aconf->confClass = find_class(0);
1128 if (aconf->status & CONF_CLIENT) {
1129 struct ConfItem *bconf;
1131 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1133 bconf->status &= ~CONF_ILLEGAL;
1134 if (aconf->status == CONF_CLIENT) {
1136 * copy the password field in case it changed
1138 MyFree(bconf->passwd);
1139 bconf->passwd = aconf->passwd;
1142 ConfLinks(bconf) -= bconf->clients;
1143 bconf->confClass = aconf->confClass;
1144 if (bconf->confClass)
1145 ConfLinks(bconf) += bconf->clients;
1151 if (aconf->status & CONF_SERVER) {
1152 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1153 strchr(aconf->host, '?') || !aconf->name)
1156 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1157 if (!strchr(aconf->host, '@')) {
1159 int len = 3; /* *@\0 = 3 */
1161 len += strlen(aconf->host);
1162 newhost = (char*) MyMalloc(len);
1163 assert(0 != newhost);
1164 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1165 MyFree(aconf->host);
1166 aconf->host = newhost;
1169 if (aconf->status & CONF_SERVER) {
1170 if (EmptyString(aconf->passwd))
1172 lookup_confhost(aconf);
1174 if (aconf->status == CONF_IPKILL) {
1176 * Here we use the same kludge as in listener.c to parse
1177 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
1183 class=sscanf(aconf->host,"%d.%d.%d.%d/%d",
1184 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
1186 aconf->bits=class*8;
1191 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0], ad[1], ad[2], ad[3]);
1193 /* This ensures endian correctness */
1194 aconf->ipnum.s_addr = inet_addr(ipname);
1195 Debug((DEBUG_DEBUG,"IPkill: %s = %08x/%i (%08x)",ipname,
1196 aconf->ipnum.s_addr,aconf->bits,NETMASK(aconf->bits)));
1200 * Juped nicks are listed in the 'password' field of U:lines,
1201 * the list is comma separated and might be empty and/or contain
1202 * empty elements... the only limit is that it MUST be shorter
1203 * than 512 chars, or they will be cutted out :)
1205 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1206 addNickJupes(aconf->passwd);
1208 collapse(aconf->host);
1209 collapse(aconf->name);
1210 Debug((DEBUG_NOTICE,
1211 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1212 aconf->status, aconf->host, aconf->passwd,
1213 aconf->name, aconf->port, aconf->confClass));
1214 aconf->next = GlobalConfList;
1215 GlobalConfList = aconf;
1221 nextping = nextconnect = CurrentTime;
1228 * Actual REHASH service routine. Called with sig == 0 if it has been called
1229 * as a result of an operator issuing this command, else assume it has been
1230 * called as a result of the server receiving a HUP signal.
1232 int rehash(struct Client *cptr, int sig)
1234 struct ConfItem** tmp = &GlobalConfList;
1235 struct ConfItem* tmp2;
1236 struct Client* acptr;
1237 struct MotdItem* temp;
1243 sendto_opmask_butone(0, SNO_OLDSNO,
1244 "Got signal SIGHUP, reloading ircd conf. file");
1246 while ((tmp2 = *tmp)) {
1247 if (tmp2->clients) {
1249 * Configuration entry is still in use by some
1250 * local clients, cannot delete it--mark it so
1251 * that it will be deleted when the last client
1254 if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1260 tmp2->status |= CONF_ILLEGAL;
1267 conf_erase_motd_list(&motdConfList);
1268 conf_erase_crule_list();
1271 * delete the juped nicks list
1276 flush_resolver_cache();
1278 class_mark_delete();
1279 mark_listeners_closing();
1281 read_configuration_file();
1284 class_delete_marked(); /* unless it fails */
1287 * Flush out deleted I and P lines although still in use.
1289 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1290 if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1299 for (i = 0; i <= HighestFd; i++) {
1300 if ((acptr = LocalClientArray[i])) {
1301 assert(!IsMe(acptr));
1302 if (IsServer(acptr)) {
1303 det_confs_butmask(acptr,
1304 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1305 attach_confs_byname(acptr, acptr->name,
1306 CONF_HUB | CONF_LEAF | CONF_UWORLD);
1308 /* Because admin's are getting so uppity about people managing to
1309 * get past K/G's etc, we'll "fix" the bug by actually explaining
1312 if ((found_g = find_kill(acptr))) {
1313 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1314 found_g == -2 ? "G-line active for %s%s" :
1315 "K-line active for %s%s",
1316 IsUnknown(acptr) ? "Unregistered Client ":"",
1317 get_client_name(acptr, HIDE_IP));
1318 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1319 "K-lined") == CPTR_KILLED)
1325 * free old motd structs
1337 /* reload motd files */
1339 rmotd = read_motd(RPATH);
1340 motd = read_motd(MPATH);
1347 * Read configuration file.
1349 * returns 0, if file cannot be opened
1355 if (read_configuration_file()) {
1357 * make sure we're sane to start if the config
1358 * file read didn't get everything we need.
1359 * XXX - should any of these abort the server?
1360 * TODO: add warning messages
1362 if (0 == localConf.name || 0 == localConf.numeric)
1365 if (0 == localConf.location1)
1366 DupString(localConf.location1, "");
1367 if (0 == localConf.location2)
1368 DupString(localConf.location2, "");
1369 if (0 == localConf.contact)
1370 DupString(localConf.contact, "");
1379 * Read info from T:lines into TRecords which include the file
1380 * timestamp, the hostmask, and the contents of the motd file
1385 struct MotdConf* conf;
1386 struct TRecord *temp;
1387 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1388 struct MotdItem *amotd;
1390 /* Free the old trecords and the associated motd contents first */
1393 while (tdata->tmotd) {
1394 amotd = tdata->tmotd->next;
1395 MyFree(tdata->tmotd);
1396 tdata->tmotd = amotd;
1402 for (conf = motdConfList; conf; conf = conf->next) {
1403 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1406 temp->hostmask = conf->hostmask;
1407 temp->tmotd = read_motd(conf->path);
1408 temp->tmotd_tm = motd_tm;
1424 * 0: Client may continue to try and connect
1425 * -1: Client was K/k:'d - sideeffect: reason was sent.
1426 * -2: Client was G/g:'d - sideeffect: reason was sent.
1428 * Client may have been sent a reason why they are denied, as above.
1430 int find_kill(struct Client *cptr)
1434 struct ConfItem* tmp;
1435 struct Gline* agline = NULL;
1442 host = cptr->sockhost;
1443 name = cptr->user->username;
1445 assert(strlen(host) <= HOSTLEN);
1446 assert((name ? strlen(name) : 0) <= HOSTLEN);
1448 /* 2000-07-14: Rewrote this loop for massive speed increases.
1451 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1453 if ((tmp->status & CONF_KLINE) == 0)
1457 * What is this for? You can K: by port?!
1460 if (tmp->port && tmp->port != cptr->listener->port)
1463 if (!tmp->name || match(tmp->name, name) != 0)
1469 if (tmp->status != CONF_IPKILL) {
1470 if (match(tmp->host, host) == 0)
1473 else { /* k: by IP */
1474 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1475 cptr->ip.s_addr, tmp->ipnum.s_addr, tmp->bits,
1476 NETMASK(tmp->bits)));
1477 if ((cptr->ip.s_addr & NETMASK(tmp->bits)) == tmp->ipnum.s_addr)
1482 if (EmptyString(tmp->passwd))
1483 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1484 ":Connection from your host is refused on this server.");
1486 if (*tmp->passwd == '"') {
1487 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1488 strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1490 else if (*tmp->passwd == '!')
1491 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1493 #ifdef COMMENT_IS_FILE
1494 killcomment(cptr, cptr->name, tmp->passwd);
1496 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1502 /* find active glines */
1503 /* added a check against the user's IP address to find_gline() -Kev */
1504 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1505 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1506 GlineReason(agline));
1508 agline = NULL; /* if a gline was found, it was inactive */
1518 struct MotdItem* read_motd(const char* motdfile)
1520 FBFILE* file = NULL;
1521 struct MotdItem* temp;
1522 struct MotdItem* newmotd;
1523 struct MotdItem* last;
1528 if (NULL == (file = fbopen(motdfile, "r"))) {
1529 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1532 if (-1 == fbstat(&sb, file)) {
1536 newmotd = last = NULL;
1537 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1538 while (fbgets(line, sizeof(line) - 1, file)) {
1539 if ((tmp = (char *)strchr(line, '\n')))
1541 if ((tmp = (char *)strchr(line, '\r')))
1543 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1545 strcpy(temp->line, line);
1559 * Ordinary client access check. Look for conf lines which have the same
1560 * status as the flags passed.
1562 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1564 enum AuthorizationCheckResult acr = ACR_OK;
1568 if ((acr = attach_iline(cptr))) {
1569 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1570 cptr->name, cptr->sockhost));
1579 * Check access for a server given its name (passed in cptr struct).
1580 * Must check for all C/N lines which have a name which matches the
1581 * name given and a host which matches. A host alias which is the
1582 * same as the server name is also acceptable in the host field of a
1587 * -1 = Access denied
1590 int conf_check_server(struct Client *cptr)
1592 struct ConfItem* c_conf = NULL;
1595 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1596 cptr->name, cptr->sockhost));
1598 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1599 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1604 * We initiated this connection so the client should have a C and N
1605 * line already attached after passing through the connect_server()
1608 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1609 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1611 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1613 det_confs_butmask(cptr, 0);
1621 if (cptr->dns_reply) {
1623 struct hostent* hp = cptr->dns_reply->hp;
1624 const char* name = hp->h_name;
1626 * If we are missing a C or N line from above, search for
1627 * it under all known hostnames we have for this ip#.
1629 for (i = 0; name; name = hp->h_aliases[i++]) {
1630 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1631 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1636 for (i = 0; hp->h_addr_list[i]; i++) {
1637 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1644 * Check for C lines with the hostname portion the ip number
1645 * of the host the server runs on. This also checks the case where
1646 * there is a server connecting from 'localhost'.
1648 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1652 * Attach by IP# only if all other checks have failed.
1653 * It is quite possible to get here with the strange things that can
1654 * happen when using DNS in the way the irc server does. -avalon
1657 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1659 * detach all conf lines that got attached by attach_confs()
1661 det_confs_butmask(cptr, 0);
1663 * if no C or no N lines, then deny access
1666 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1667 cptr->name, cptr->username, cptr->sockhost));
1670 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1672 * attach the C and N lines to the client structure for later use.
1674 attach_conf(cptr, c_conf);
1675 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1677 if (INADDR_NONE == c_conf->ipnum.s_addr)
1678 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1680 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));