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.
29 #include "ircd_features.h"
34 #include "ircd_alloc.h"
35 #include "ircd_chattr.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #include "ircd_string.h"
58 #include <arpa/inet.h>
68 #define INADDR_NONE 0xffffffff
71 struct ConfItem* GlobalConfList = 0;
72 int GlobalConfCount = 0;
77 struct LocalConf localConf;
78 struct CRuleConf* cruleConfList;
79 /* struct ServerConf* serverConfList; */
80 struct DenyConf* denyConfList;
83 * output the reason for being k lined from a file - Mmmm
84 * sptr is client being dumped
85 * filename is the file that is to be output to the K lined client
87 static void killcomment(struct Client* sptr, const 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 char* end = line + strlen(line);
106 if ('\n' == *end || '\r' == *end)
111 send_reply(sptr, RPL_MOTD, line);
113 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
114 ":Connection from your host is refused on this server.");
118 struct ConfItem* make_conf(void)
120 struct ConfItem* aconf;
122 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
127 memset(aconf, 0, sizeof(struct ConfItem));
128 aconf->status = CONF_ILLEGAL;
129 aconf->ipnum.s_addr = INADDR_NONE;
133 void delist_conf(struct ConfItem *aconf)
135 if (aconf == GlobalConfList)
136 GlobalConfList = GlobalConfList->next;
138 struct ConfItem *bconf;
140 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
142 bconf->next = aconf->next;
147 void free_conf(struct ConfItem *aconf)
149 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
150 aconf->host ? aconf->host : "*",
151 aconf->name ? aconf->name : "*",
153 if (aconf->dns_pending)
154 delete_resolver_queries(aconf);
157 memset(aconf->passwd, 0, strlen(aconf->passwd));
158 MyFree(aconf->passwd);
167 * detach_conf - Disassociate configuration from the client.
169 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
176 assert(0 < aconf->clients);
178 lp = &(cli_confs(cptr));
181 if ((*lp)->value.aconf == aconf) {
182 if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
185 assert(0 < aconf->clients);
186 if (0 == --aconf->clients && IsIllegal(aconf))
198 * conf_dns_callback - called when resolver query finishes
199 * if the query resulted in a successful search, hp will contain
200 * a non-null pointer, otherwise hp will be null.
201 * if successful save hp in the conf item it was called with
203 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
205 struct ConfItem* aconf = (struct ConfItem*) vptr;
206 aconf->dns_pending = 0;
208 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
212 * conf_dns_lookup - do a nameserver lookup of the conf host
213 * if the conf entry is currently doing a ns lookup do nothing, otherwise
214 * if the lookup returns a null pointer, set the conf dns_pending flag
216 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
218 struct DNSReply* dns_reply = 0;
219 if (!aconf->dns_pending) {
220 char buf[HOSTLEN + 1];
221 struct DNSQuery query;
223 query.callback = conf_dns_callback;
224 host_from_uh(buf, aconf->host, HOSTLEN);
227 if (0 == (dns_reply = gethost_byname(buf, &query)))
228 aconf->dns_pending = 1;
237 * Do (start) DNS lookups of all hostnames in the conf line and convert
238 * an IP addresses in a.b.c.d number for to IP#s.
240 void lookup_confhost(struct ConfItem *aconf)
242 struct DNSReply* reply;
244 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
245 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
246 aconf->host, aconf->name));
250 * Do name lookup now on hostnames given and store the
251 * ip numbers in conf structure.
253 if (IsDigit(*aconf->host)) {
255 * rfc 1035 sez host names may not start with a digit
256 * XXX - this has changed code needs to be updated
258 aconf->ipnum.s_addr = inet_addr(aconf->host);
259 if (INADDR_NONE == aconf->ipnum.s_addr) {
260 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
261 aconf->host, aconf->name));
264 else if ((reply = conf_dns_lookup(aconf)))
265 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
269 * conf_find_server - find a server by name or hostname
270 * returns a server conf item pointer if found, 0 otherwise
272 * NOTE: at some point when we don't have to scan the entire
273 * list it may be cheaper to look for server names and host
274 * names in separate loops (original code did it that way)
276 struct ConfItem* conf_find_server(const char* name)
278 struct ConfItem* conf;
281 for (conf = GlobalConfList; conf; conf = conf->next) {
282 if (CONF_SERVER == conf->status) {
284 * Check first servernames, then try hostnames.
285 * XXX - match returns 0 if there _is_ a match... guess they
286 * haven't decided what true is yet
288 if (0 == match(name, conf->name))
296 * conf_eval_crule - evaluate connection rules
297 * returns the name of the rule triggered if found, 0 otherwise
299 * Evaluate connection rules... If no rules found, allow the
300 * connect. Otherwise stop with the first true rule (ie: rules
301 * are ored together. Oper connects are effected only by D
302 * lines (CRULE_ALL) not d lines (CRULE_AUTO).
304 const char* conf_eval_crule(const char* name, int mask)
306 struct CRuleConf* p = cruleConfList;
309 for ( ; p; p = p->next) {
310 if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
311 if (crule_eval(p->node))
319 * Remove all conf entries from the client except those which match
320 * the status field mask.
322 void det_confs_butmask(struct Client* cptr, int mask)
328 for (link = cli_confs(cptr); link; link = next) {
330 if ((link->value.aconf->status & mask) == 0)
331 detach_conf(cptr, link->value.aconf);
336 * check_limit_and_attach - check client limits and attach I:line
338 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
339 * and dislallow more than 255 people here as well as in ipcheck.
340 * removed the old "ONE" scheme too.
341 * -- Isomer 2000-06-22
343 static enum AuthorizationCheckResult
344 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
349 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
350 number = *aconf->passwd-'0';
351 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
353 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
355 if (IPcheck_nr(cptr) > number)
356 return ACR_TOO_MANY_FROM_IP;
357 return attach_conf(cptr, aconf);
361 * Find the first (best) I line to attach.
363 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
365 struct ConfItem* aconf;
368 static char uhost[HOSTLEN + USERLEN + 3];
369 static char fullname[HOSTLEN + 1];
370 struct hostent* hp = 0;
374 if (cli_dns_reply(cptr))
375 hp = cli_dns_reply(cptr)->hp;
377 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
378 if (aconf->status != CONF_CLIENT)
380 if (aconf->port && aconf->port != cli_listener(cptr)->port)
382 if (!aconf->host || !aconf->name)
385 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
386 ircd_strncpy(fullname, hname, HOSTLEN);
387 fullname[HOSTLEN] = '\0';
389 Debug((DEBUG_DNS, "a_il: %s->%s", cli_sockhost(cptr), fullname));
391 if (strchr(aconf->name, '@')) {
392 strcpy(uhost, cli_username(cptr));
397 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
398 uhost[sizeof(uhost) - 1] = 0;
399 if (0 == match(aconf->name, uhost)) {
400 if (strchr(uhost, '@'))
401 cli_flags(cptr) |= FLAGS_DOID;
402 return check_limit_and_attach(cptr, aconf);
406 if (strchr(aconf->host, '@')) {
407 ircd_strncpy(uhost, cli_username(cptr), sizeof(uhost) - 2);
408 uhost[sizeof(uhost) - 2] = 0;
413 strncat(uhost, cli_sock_ip(cptr), sizeof(uhost) - 1 - strlen(uhost));
414 uhost[sizeof(uhost) - 1] = 0;
415 if (match(aconf->host, uhost))
417 if (strchr(uhost, '@'))
418 cli_flags(cptr) |= FLAGS_DOID;
420 return check_limit_and_attach(cptr, aconf);
422 return ACR_NO_AUTHORIZATION;
425 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
429 for (lp = cli_confs(cptr); lp; lp = lp->next) {
430 if (lp->value.aconf == aconf)
439 * Associate a specific configuration entry to a *local*
440 * client (this is the one which used in accepting the
441 * connection). Note, that this automaticly changes the
442 * attachment if there was an old one...
444 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
448 if (is_attached(aconf, cptr))
449 return ACR_ALREADY_AUTHORIZED;
450 if (IsIllegal(aconf))
451 return ACR_NO_AUTHORIZATION;
452 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
453 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
454 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
456 lp->next = cli_confs(cptr);
457 lp->value.aconf = aconf;
458 cli_confs(cptr) = lp;
460 if (aconf->status & CONF_CLIENT_MASK)
465 const struct LocalConf* conf_get_local(void)
471 * attach_confs_byname
473 * Attach a CONF line to a client if the name passed matches that for
474 * the conf file (for non-C/N lines) or is an exact match (C/N lines
475 * only). The difference in behaviour is to stop C:*::* and N:*::*.
477 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
480 struct ConfItem* tmp;
481 struct ConfItem* first = NULL;
485 if (HOSTLEN < strlen(name))
488 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
489 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
490 assert(0 != tmp->name);
491 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
492 if (ACR_OK == attach_conf(cptr, tmp) && !first)
501 * Added for new access check meLazy
503 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
506 struct ConfItem* tmp;
507 struct ConfItem* first = 0;
510 if (HOSTLEN < strlen(host))
513 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
514 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
515 assert(0 != tmp->host);
516 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
517 if (ACR_OK == attach_conf(cptr, tmp) && !first)
526 * find a conf entry which matches the hostname and has the same name.
528 struct ConfItem* find_conf_exact(const char* name, const char* user,
529 const char* host, int statmask)
531 struct ConfItem *tmp;
532 char userhost[USERLEN + HOSTLEN + 3];
535 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
537 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
539 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
540 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
541 0 != ircd_strcmp(tmp->name, name))
544 * Accept if the *real* hostname (usually sockecthost)
545 * socket host) matches *either* host or name field
546 * of the configuration.
548 if (match(tmp->host, userhost))
550 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
551 if (tmp->clients < MaxLinks(tmp->conn_class))
562 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
565 struct ConfItem* tmp;
568 if (HOSTLEN < strlen(name))
571 for (; lp; lp = lp->next) {
572 tmp = lp->value.aconf;
573 if (0 != (tmp->status & statmask)) {
574 assert(0 != tmp->name);
575 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
583 * Added for new access check meLazy
585 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
588 struct ConfItem* tmp = NULL;
591 if (HOSTLEN < strlen(host))
594 for (; lp; lp = lp->next) {
595 tmp = lp->value.aconf;
596 if (0 != (tmp->status & statmask)) {
597 assert(0 != tmp->host);
598 if (0 == match(tmp->host, host))
608 * Find a conf line using the IP# stored in it to search upon.
609 * Added 1/8/92 by Avalon.
611 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
614 struct ConfItem* tmp;
616 for (; lp; lp = lp->next) {
617 tmp = lp->value.aconf;
618 if (0 != (tmp->status & statmask)) {
619 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
629 * - looks for a match on all given fields.
632 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
635 struct ConfItem *bconf;
638 mask &= ~CONF_ILLEGAL;
640 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
641 if (!(bconf->status & mask) || (bconf->port != aconf->port))
644 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
645 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
647 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
650 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
651 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
653 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
654 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
657 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
658 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
660 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
668 * If conf line is a class definition, create a class entry
669 * for it and make the conf_line illegal and delete it.
671 void conf_add_class(const char* const* fields, int count)
675 add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
676 atoi(fields[4]), atoi(fields[5]));
679 void conf_add_listener(const char* const* fields, int count)
687 if (count < 5 || EmptyString(fields[4]))
690 if (!EmptyString(fields[3])) {
691 const char* x = fields[3];
692 if ('S' == ToUpper(*x))
695 if ('H' == ToUpper(*x))
698 /* port vhost mask */
699 add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
702 void conf_add_local(const char* const* fields, int count)
704 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[5])) {
705 log_write(LS_CONFIG, L_CRIT, 0, "Your M: line must have 6 fields!");
709 * these two can only be set the first time
711 if (0 == localConf.name) {
712 if (string_is_hostname(fields[1]))
713 DupString(localConf.name, fields[1]);
715 if (0 == localConf.numeric) {
716 localConf.numeric = atoi(fields[5]);
717 if (0 == localConf.numeric)
718 log_write(LS_CONFIG, L_WARNING, 0,
719 "Your M: line must have a Numeric value greater than 0");
722 * these two can be changed while the server is running
724 if (string_is_address(fields[2])) {
725 if (INADDR_NONE == (localConf.vhost_address.s_addr = inet_addr(fields[2])))
726 localConf.vhost_address.s_addr = INADDR_ANY;
728 MyFree(localConf.description);
729 DupString(localConf.description, fields[3]);
731 * XXX - shouldn't be setting these directly here
733 ircd_strncpy(cli_info(&me), fields[3], REALLEN);
734 set_virtual_host(localConf.vhost_address);
737 void conf_add_admin(const char* const* fields, int count)
740 * if you have one, it MUST have 3 lines
743 log_write(LS_CONFIG, L_CRIT, 0, "Your A: line must have 4 fields!");
746 MyFree(localConf.location1);
747 DupString(localConf.location1, fields[1]);
749 MyFree(localConf.location2);
750 DupString(localConf.location2, fields[2]);
752 MyFree(localConf.contact);
753 DupString(localConf.contact, fields[3]);
757 * conf_add_crule - Create expression tree from connect rule and add it
760 void conf_add_crule(const char* const* fields, int count, int type)
762 struct CRuleNode* node;
765 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
768 if ((node = crule_parse(fields[3]))) {
769 struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
772 DupString(p->hostmask, fields[1]);
773 collapse(p->hostmask);
775 DupString(p->rule, fields[3]);
779 p->next = cruleConfList;
785 void conf_erase_crule_list(void)
787 struct CRuleConf* next;
788 struct CRuleConf* p = cruleConfList;
790 for ( ; p; p = next) {
792 crule_free(&p->node);
800 const struct CRuleConf* conf_get_crule_list(void)
802 return cruleConfList;
806 void conf_add_server(const char* const* fields, int count)
808 struct ServerConf* server;
812 * missing host, password, or alias?
814 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
819 if (string_is_hostname(fields[1]))
820 addr.s_addr = INADDR_NONE;
821 else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
824 server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
826 DupString(server->hostname, fields[1]);
827 DupString(server->passwd, fields[2]);
828 DupString(server->alias, fields[3]);
829 server->address.s_addr = addr.s_addr;
830 server->port = atoi(fields[4]);
831 server->dns_pending = 0;
832 server->connected = 0;
834 server->conn_class = find_class(atoi(fields[5]));
836 server->next = serverConfList;
837 serverConfList = server;
839 /* if (INADDR_NONE == server->address.s_addr) */
840 /* lookup_confhost(server); */
843 void conf_add_deny(const char* const* fields, int count, int ip_kill)
845 struct DenyConf* conf;
847 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
850 conf = (struct DenyConf*) MyMalloc(sizeof(struct DenyConf));
852 memset(conf, 0, sizeof(struct DenyConf));
854 if (fields[1][0] == '$' && fields[1][1] == 'R')
855 conf->flags |= DENY_FLAGS_REALNAME;
857 DupString(conf->hostmask, fields[1]);
858 collapse(conf->hostmask);
860 if (!EmptyString(fields[2])) {
861 const char* p = fields[2];
863 conf->flags |= DENY_FLAGS_FILE;
866 DupString(conf->message, p);
868 DupString(conf->usermask, fields[3]);
869 collapse(conf->usermask);
873 * Here we use the same kludge as in listener.c to parse
874 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
880 c_class = sscanf(conf->hostmask, "%d.%d.%d.%d/%d",
881 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
883 conf->bits = c_class * 8;
888 ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1],
892 * This ensures endian correctness
894 conf->address = inet_addr(ipname);
895 Debug((DEBUG_DEBUG, "IPkill: %s = %08x/%i (%08x)", ipname,
896 conf->address, conf->bits, NETMASK(conf->bits)));
897 conf->flags |= DENY_FLAGS_IP;
899 conf->next = denyConfList;
905 void conf_erase_deny_list(void)
907 struct DenyConf* next;
908 struct DenyConf* p = denyConfList;
909 for ( ; p; p = next) {
919 const struct DenyConf* conf_get_deny_list(void)
925 * read_configuration_file
927 * Read configuration file.
929 * returns 0, if file cannot be opened
933 #define MAXCONFLINKS 150
935 int read_configuration_file(void)
937 feature_unmark(); /* unmark all features for resetting later */
938 /* Now just open an fd. The buffering isn't really needed... */
939 if ((conf_fd = open(configfile, O_RDONLY)) < 0)
945 while (fbgets(line, sizeof(line) - 1, file)) {
946 if ('#' == *line || IsSpace(*line))
949 if ((src = strchr(line, '\n')))
952 if (':' != line[1]) {
953 Debug((DEBUG_ERROR, "Bad config line: %s", line));
954 sendto_opmask_butone(0, SNO_OLDSNO,"Bad Config line");
959 * do escapes, quoting, comments, and field breakup in place
960 * in one pass with a poor mans state machine
962 field_vector[0] = line;
966 for (src = line, dest = line; *src; ) {
1021 field_vector[field_count++] = dest;
1022 if (field_count > MAX_FIELDS)
1038 if (field_count < 2 || EmptyString(field_vector[0]))
1044 aconf = make_conf();
1046 switch (*field_vector[0]) {
1047 case 'A': /* Name, e-mail address of administrator */
1048 case 'a': /* of this server. CONF_ADMIN */
1049 conf_add_admin(field_vector, field_count);
1050 aconf->status = CONF_ILLEGAL;
1052 case 'C': /* Server where I should try to connect */
1053 case 'c': /* in case of lp failures */
1055 aconf->status = CONF_SERVER;
1058 case 'D': /* CONF_CRULEALL */
1059 conf_add_crule(field_vector, field_count, CRULE_ALL);
1060 aconf->status = CONF_ILLEGAL;
1062 /* Connect rule - autos only */
1063 case 'd': /* CONF_CRULEAUTO */
1064 conf_add_crule(field_vector, field_count, CRULE_AUTO);
1065 aconf->status = CONF_ILLEGAL;
1067 case 'F': /* Feature line */
1069 feature_set(0, &field_vector[1], field_count - 1);
1070 aconf->status = CONF_ILLEGAL;
1072 case 'H': /* Hub server line */
1074 aconf->status = CONF_HUB;
1076 case 'I': /* Just plain normal irc client trying */
1077 case 'i': /* to connect me */
1078 aconf->status = CONF_CLIENT;
1080 case 'K': /* Kill user line on irc.conf */
1081 conf_add_deny(field_vector, field_count, 0);
1082 aconf->status = CONF_ILLEGAL;
1084 case 'k': /* Kill user line based on IP in ircd.conf */
1085 conf_add_deny(field_vector, field_count, 1);
1086 aconf->status = CONF_ILLEGAL;
1088 /* Operator. Line should contain at least */
1089 /* password and host where connection is */
1090 case 'L': /* guaranteed leaf server */
1092 aconf->status = CONF_LEAF;
1094 /* Me. Host field is name used for this host */
1095 /* and port number is the number of the port */
1097 case 'm': /* CONF_ME */
1098 conf_add_local(field_vector, field_count);
1099 aconf->status = CONF_ILLEGAL;
1102 aconf->status = CONF_OPERATOR;
1104 /* Local Operator, (limited privs --SRB) */
1106 aconf->status = CONF_LOCOP;
1108 case 'P': /* listen port line */
1109 case 'p': /* CONF_LISTEN_PORT */
1110 conf_add_listener(field_vector, field_count);
1111 aconf->status = CONF_ILLEGAL;
1113 case 'T': /* print out different motd's */
1114 case 't': /* based on hostmask - CONF_TLINES */
1115 motd_add(field_vector[1], field_vector[2]);
1116 aconf->status = CONF_ILLEGAL;
1118 case 'U': /* Underworld server, allowed to hack modes */
1119 case 'u': /* *Every* server on the net must define the same !!! */
1120 aconf->status = CONF_UWORLD;
1123 case 'y': /* CONF_CLASS */
1124 conf_add_class(field_vector, field_count);
1125 aconf->status = CONF_ILLEGAL;
1128 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1129 sendto_opmask_butone(0, SNO_OLDSNO, "Unknown prefix in config file: %c",
1131 aconf->status = CONF_ILLEGAL;
1134 if (IsIllegal(aconf))
1137 if (!EmptyString(field_vector[1]))
1138 DupString(aconf->host, field_vector[1]);
1140 if (!EmptyString(field_vector[2]))
1141 DupString(aconf->passwd, field_vector[2]);
1143 if (field_count > 3 && !EmptyString(field_vector[3]))
1144 DupString(aconf->name, field_vector[3]);
1146 if (field_count > 4 && !EmptyString(field_vector[4]))
1147 aconf->port = atoi(field_vector[4]);
1149 if (field_count > 5 && !EmptyString(field_vector[5]))
1150 aconf->conn_class = find_class(atoi(field_vector[5]));
1153 * Associate each conf line with a class by using a pointer
1154 * to the correct class record. -avalon
1156 if (aconf->status & CONF_CLIENT_MASK) {
1157 if (aconf->conn_class == 0)
1158 aconf->conn_class = find_class(0);
1160 if (aconf->status & CONF_CLIENT) {
1161 struct ConfItem *bconf;
1163 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1165 bconf->status &= ~CONF_ILLEGAL;
1166 if (aconf->status == CONF_CLIENT) {
1168 * copy the password field in case it changed
1170 MyFree(bconf->passwd);
1171 bconf->passwd = aconf->passwd;
1174 ConfLinks(bconf) -= bconf->clients;
1175 bconf->conn_class = aconf->conn_class;
1176 if (bconf->conn_class)
1177 ConfLinks(bconf) += bconf->clients;
1183 if (aconf->status & CONF_SERVER) {
1184 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1185 strchr(aconf->host, '?') || !aconf->name)
1188 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1189 if (!strchr(aconf->host, '@')) {
1191 int len = 3; /* *@\0 = 3 */
1193 len += strlen(aconf->host);
1194 newhost = (char*) MyMalloc(len);
1195 assert(0 != newhost);
1196 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1197 MyFree(aconf->host);
1198 aconf->host = newhost;
1201 if (aconf->status & CONF_SERVER) {
1202 if (EmptyString(aconf->passwd))
1204 lookup_confhost(aconf);
1207 * Juped nicks are listed in the 'password' field of U:lines,
1208 * the list is comma separated and might be empty and/or contain
1209 * empty elements... the only limit is that it MUST be shorter
1210 * than 512 chars, or they will be cutted out :)
1212 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1213 addNickJupes(aconf->passwd);
1215 collapse(aconf->host);
1216 collapse(aconf->name);
1217 Debug((DEBUG_NOTICE,
1218 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1219 aconf->status, aconf->host, aconf->passwd,
1220 aconf->name, aconf->port, aconf->conn_class));
1221 aconf->next = GlobalConfList;
1222 GlobalConfList = aconf;
1228 /* nextping = nextconnect = CurrentTime; */
1230 feature_mark(); /* reset unmarked features */
1235 yyerror(const char *msg)
1237 sendto_opmask_butone(0, SNO_ALL, "Config file parse error line %d: %s",
1239 log_write(LS_CONFIG, L_ERROR, 0, "Config file parse error line %d: %s",
1246 * Actual REHASH service routine. Called with sig == 0 if it has been called
1247 * as a result of an operator issuing this command, else assume it has been
1248 * called as a result of the server receiving a HUP signal.
1250 int rehash(struct Client *cptr, int sig)
1252 struct ConfItem** tmp = &GlobalConfList;
1253 struct ConfItem* tmp2;
1254 struct Client* acptr;
1260 sendto_opmask_butone(0, SNO_OLDSNO,
1261 "Got signal SIGHUP, reloading ircd conf. file");
1263 while ((tmp2 = *tmp)) {
1264 if (tmp2->clients) {
1266 * Configuration entry is still in use by some
1267 * local clients, cannot delete it--mark it so
1268 * that it will be deleted when the last client
1271 if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1277 tmp2->status |= CONF_ILLEGAL;
1284 conf_erase_crule_list();
1285 conf_erase_deny_list();
1289 * delete the juped nicks list
1294 flush_resolver_cache();
1296 class_mark_delete();
1297 mark_listeners_closing();
1299 read_configuration_file();
1301 log_reopen(); /* reopen log files */
1304 class_delete_marked(); /* unless it fails */
1307 * Flush out deleted I and P lines although still in use.
1309 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1310 if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1320 for (i = 0; i <= HighestFd; i++) {
1321 if ((acptr = LocalClientArray[i])) {
1322 assert(!IsMe(acptr));
1323 if (IsServer(acptr)) {
1324 det_confs_butmask(acptr,
1325 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1326 attach_confs_byname(acptr, cli_name(acptr),
1327 CONF_HUB | CONF_LEAF | CONF_UWORLD);
1329 /* Because admin's are getting so uppity about people managing to
1330 * get past K/G's etc, we'll "fix" the bug by actually explaining
1333 if ((found_g = find_kill(acptr))) {
1334 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1335 found_g == -2 ? "G-line active for %s%s" :
1336 "K-line active for %s%s",
1337 IsUnknown(acptr) ? "Unregistered Client ":"",
1338 get_client_name(acptr, HIDE_IP));
1339 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1340 "K-lined") == CPTR_KILLED)
1352 * Read configuration file.
1354 * returns 0, if file cannot be opened
1360 if (read_configuration_file()) {
1362 * make sure we're sane to start if the config
1363 * file read didn't get everything we need.
1364 * XXX - should any of these abort the server?
1365 * TODO: add warning messages
1367 if (0 == localConf.name || 0 == localConf.numeric)
1370 if (0 == localConf.location1)
1371 DupString(localConf.location1, "");
1372 if (0 == localConf.location2)
1373 DupString(localConf.location2, "");
1374 if (0 == localConf.contact)
1375 DupString(localConf.contact, "");
1387 * 0: Client may continue to try and connect
1388 * -1: Client was K/k:'d - sideeffect: reason was sent.
1389 * -2: Client was G/g:'d - sideeffect: reason was sent.
1391 * Client may have been sent a reason why they are denied, as above.
1393 int find_kill(struct Client *cptr)
1397 const char* realname;
1398 struct DenyConf* deny;
1399 struct Gline* agline = NULL;
1403 if (!cli_user(cptr))
1406 host = cli_sockhost(cptr);
1407 name = cli_user(cptr)->username;
1408 realname = cli_info(cptr);
1410 assert(strlen(host) <= HOSTLEN);
1411 assert((name ? strlen(name) : 0) <= HOSTLEN);
1412 assert((realname ? strlen(realname) : 0) <= REALLEN);
1414 /* 2000-07-14: Rewrote this loop for massive speed increases.
1417 for (deny = denyConfList; deny; deny = deny->next) {
1418 if (0 != match(deny->usermask, name))
1421 if (EmptyString(deny->hostmask))
1424 if (deny->flags & DENY_FLAGS_REALNAME) { /* K: by real name */
1425 if (0 == match(deny->hostmask, realname))
1427 } else if (deny->flags & DENY_FLAGS_IP) { /* k: by IP */
1428 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1429 cli_ip(cptr).s_addr, deny->address, deny->bits, NETMASK(deny->bits)));
1430 if ((cli_ip(cptr).s_addr & NETMASK(deny->bits)) == deny->address)
1433 else if (0 == match(deny->hostmask, host))
1437 if (EmptyString(deny->message))
1438 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1439 ":Connection from your host is refused on this server.");
1441 if (deny->flags & DENY_FLAGS_FILE)
1442 killcomment(cptr, deny->message);
1444 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
1447 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline)) {
1449 * find active glines
1450 * added a check against the user's IP address to find_gline() -Kev
1452 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
1455 agline = 0; /* if a gline was found, it was inactive */
1466 * Ordinary client access check. Look for conf lines which have the same
1467 * status as the flags passed.
1469 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1471 enum AuthorizationCheckResult acr = ACR_OK;
1475 if ((acr = attach_iline(cptr))) {
1476 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1477 cli_name(cptr), cli_sockhost(cptr)));
1486 * Check access for a server given its name (passed in cptr struct).
1487 * Must check for all C/N lines which have a name which matches the
1488 * name given and a host which matches. A host alias which is the
1489 * same as the server name is also acceptable in the host field of a
1494 * -1 = Access denied
1497 int conf_check_server(struct Client *cptr)
1499 struct ConfItem* c_conf = NULL;
1502 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1503 cli_name(cptr), cli_sockhost(cptr)));
1505 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
1506 Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
1509 lp = cli_confs(cptr);
1511 * We initiated this connection so the client should have a C and N
1512 * line already attached after passing through the connect_server()
1515 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1516 c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1518 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1520 det_confs_butmask(cptr, 0);
1528 if (cli_dns_reply(cptr)) {
1530 struct hostent* hp = cli_dns_reply(cptr)->hp;
1531 const char* name = hp->h_name;
1533 * If we are missing a C or N line from above, search for
1534 * it under all known hostnames we have for this ip#.
1536 for (i = 0; name; name = hp->h_aliases[i++]) {
1537 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1538 ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1543 for (i = 0; hp->h_addr_list[i]; i++) {
1544 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1551 * Check for C lines with the hostname portion the ip number
1552 * of the host the server runs on. This also checks the case where
1553 * there is a server connecting from 'localhost'.
1555 c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1559 * Attach by IP# only if all other checks have failed.
1560 * It is quite possible to get here with the strange things that can
1561 * happen when using DNS in the way the irc server does. -avalon
1564 c_conf = find_conf_byip(lp, (const char*) &(cli_ip(cptr)), CONF_SERVER);
1566 * detach all conf lines that got attached by attach_confs()
1568 det_confs_butmask(cptr, 0);
1570 * if no C or no N lines, then deny access
1573 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1574 cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1577 ircd_strncpy(cli_name(cptr), c_conf->name, HOSTLEN);
1579 * attach the C and N lines to the client structure for later use.
1581 attach_conf(cptr, c_conf);
1582 attach_confs_byname(cptr, cli_name(cptr), CONF_HUB | CONF_LEAF | CONF_UWORLD);
1584 if (INADDR_NONE == c_conf->ipnum.s_addr)
1585 c_conf->ipnum.s_addr = cli_ip(cptr).s_addr;
1587 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cli_name(cptr), cli_sockhost(cptr)));