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.
28 #include "ircd_features.h"
33 #include "ircd_alloc.h"
34 #include "ircd_chattr.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
52 #include "sprintf_irc.h"
58 #include <arpa/inet.h>
69 #define INADDR_NONE 0xffffffff
72 struct ConfItem* GlobalConfList = 0;
73 int GlobalConfCount = 0;
75 static struct LocalConf localConf;
76 static struct CRuleConf* cruleConfList;
77 static struct ServerConf* serverConfList;
78 static struct DenyConf* denyConfList;
81 * output the reason for being k lined from a file - Mmmm
82 * sptr is client being dumped
83 * filename is the file that is to be output to the K lined client
85 static void killcomment(struct Client* sptr, const char* filename)
92 if (NULL == (file = fbopen(filename, "r"))) {
93 send_reply(sptr, ERR_NOMOTD);
94 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
95 ":Connection from your host is refused on this server.");
99 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
100 while (fbgets(line, sizeof(line) - 1, file)) {
101 char* end = line + strlen(line);
104 if ('\n' == *end || '\r' == *end)
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 memset(aconf, 0, sizeof(struct ConfItem));
126 aconf->status = CONF_ILLEGAL;
127 aconf->ipnum.s_addr = INADDR_NONE;
131 void delist_conf(struct ConfItem *aconf)
133 if (aconf == GlobalConfList)
134 GlobalConfList = GlobalConfList->next;
136 struct ConfItem *bconf;
138 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
140 bconf->next = aconf->next;
145 void free_conf(struct ConfItem *aconf)
147 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
148 aconf->host ? aconf->host : "*",
149 aconf->name ? aconf->name : "*",
151 if (aconf->dns_pending)
152 delete_resolver_queries(aconf);
155 memset(aconf->passwd, 0, strlen(aconf->passwd));
156 MyFree(aconf->passwd);
165 * detach_conf - Disassociate configuration from the client.
167 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
174 assert(0 < aconf->clients);
176 lp = &(cli_confs(cptr));
179 if ((*lp)->value.aconf == aconf) {
180 if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
183 assert(0 < aconf->clients);
184 if (0 == --aconf->clients && IsIllegal(aconf))
196 * conf_dns_callback - called when resolver query finishes
197 * if the query resulted in a successful search, hp will contain
198 * a non-null pointer, otherwise hp will be null.
199 * if successful save hp in the conf item it was called with
201 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
203 struct ConfItem* aconf = (struct ConfItem*) vptr;
204 aconf->dns_pending = 0;
206 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
210 * conf_dns_lookup - do a nameserver lookup of the conf host
211 * if the conf entry is currently doing a ns lookup do nothing, otherwise
212 * if the lookup returns a null pointer, set the conf dns_pending flag
214 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
216 struct DNSReply* dns_reply = 0;
217 if (!aconf->dns_pending) {
218 char buf[HOSTLEN + 1];
219 struct DNSQuery query;
221 query.callback = conf_dns_callback;
222 host_from_uh(buf, aconf->host, HOSTLEN);
225 if (0 == (dns_reply = gethost_byname(buf, &query)))
226 aconf->dns_pending = 1;
235 * Do (start) DNS lookups of all hostnames in the conf line and convert
236 * an IP addresses in a.b.c.d number for to IP#s.
238 static void lookup_confhost(struct ConfItem *aconf)
240 struct DNSReply* reply;
242 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
243 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
244 aconf->host, aconf->name));
248 * Do name lookup now on hostnames given and store the
249 * ip numbers in conf structure.
251 if (IsDigit(*aconf->host)) {
253 * rfc 1035 sez host names may not start with a digit
254 * XXX - this has changed code needs to be updated
256 aconf->ipnum.s_addr = inet_addr(aconf->host);
257 if (INADDR_NONE == aconf->ipnum.s_addr) {
258 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
259 aconf->host, aconf->name));
262 else if ((reply = conf_dns_lookup(aconf)))
263 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
267 * conf_find_server - find a server by name or hostname
268 * returns a server conf item pointer if found, 0 otherwise
270 * NOTE: at some point when we don't have to scan the entire
271 * list it may be cheaper to look for server names and host
272 * names in separate loops (original code did it that way)
274 struct ConfItem* conf_find_server(const char* name)
276 struct ConfItem* conf;
279 for (conf = GlobalConfList; conf; conf = conf->next) {
280 if (CONF_SERVER == conf->status) {
282 * Check first servernames, then try hostnames.
283 * XXX - match returns 0 if there _is_ a match... guess they
284 * haven't decided what true is yet
286 if (0 == match(name, conf->name))
294 * conf_eval_crule - evaluate connection rules
295 * returns the name of the rule triggered if found, 0 otherwise
297 * Evaluate connection rules... If no rules found, allow the
298 * connect. Otherwise stop with the first true rule (ie: rules
299 * are ored together. Oper connects are effected only by D
300 * lines (CRULE_ALL) not d lines (CRULE_AUTO).
302 const char* conf_eval_crule(const char* name, int mask)
304 struct CRuleConf* p = cruleConfList;
307 for ( ; p; p = p->next) {
308 if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
309 if (crule_eval(p->node))
317 * Remove all conf entries from the client except those which match
318 * the status field mask.
320 void det_confs_butmask(struct Client* cptr, int mask)
326 for (link = cli_confs(cptr); link; link = next) {
328 if ((link->value.aconf->status & mask) == 0)
329 detach_conf(cptr, link->value.aconf);
334 * check_limit_and_attach - check client limits and attach I:line
336 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
337 * and dislallow more than 255 people here as well as in ipcheck.
338 * removed the old "ONE" scheme too.
339 * -- Isomer 2000-06-22
341 static enum AuthorizationCheckResult
342 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
347 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
348 number = *aconf->passwd-'0';
349 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
351 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
353 if (IPcheck_nr(cptr) > number)
354 return ACR_TOO_MANY_FROM_IP;
355 return attach_conf(cptr, aconf);
359 * Find the first (best) I line to attach.
361 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
363 struct ConfItem* aconf;
366 static char uhost[HOSTLEN + USERLEN + 3];
367 static char fullname[HOSTLEN + 1];
368 struct hostent* hp = 0;
372 if (cli_dns_reply(cptr))
373 hp = cli_dns_reply(cptr)->hp;
375 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
376 if (aconf->status != CONF_CLIENT)
378 if (aconf->port && aconf->port != cli_listener(cptr)->port)
380 if (!aconf->host || !aconf->name)
383 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
384 ircd_strncpy(fullname, hname, HOSTLEN);
385 fullname[HOSTLEN] = '\0';
387 Debug((DEBUG_DNS, "a_il: %s->%s", cli_sockhost(cptr), fullname));
389 if (strchr(aconf->name, '@')) {
390 strcpy(uhost, cli_username(cptr));
395 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
396 uhost[sizeof(uhost) - 1] = 0;
397 if (0 == match(aconf->name, uhost)) {
398 if (strchr(uhost, '@'))
399 cli_flags(cptr) |= FLAGS_DOID;
400 return check_limit_and_attach(cptr, aconf);
404 if (strchr(aconf->host, '@')) {
405 ircd_strncpy(uhost, cli_username(cptr), sizeof(uhost) - 2);
406 uhost[sizeof(uhost) - 2] = 0;
411 strncat(uhost, cli_sock_ip(cptr), sizeof(uhost) - 1 - strlen(uhost));
412 uhost[sizeof(uhost) - 1] = 0;
413 if (match(aconf->host, uhost))
415 if (strchr(uhost, '@'))
416 cli_flags(cptr) |= FLAGS_DOID;
418 return check_limit_and_attach(cptr, aconf);
420 return ACR_NO_AUTHORIZATION;
423 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
427 for (lp = cli_confs(cptr); lp; lp = lp->next) {
428 if (lp->value.aconf == aconf)
437 * Associate a specific configuration entry to a *local*
438 * client (this is the one which used in accepting the
439 * connection). Note, that this automaticly changes the
440 * attachment if there was an old one...
442 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
446 if (is_attached(aconf, cptr))
447 return ACR_ALREADY_AUTHORIZED;
448 if (IsIllegal(aconf))
449 return ACR_NO_AUTHORIZATION;
450 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
451 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
452 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
454 lp->next = cli_confs(cptr);
455 lp->value.aconf = aconf;
456 cli_confs(cptr) = lp;
458 if (aconf->status & CONF_CLIENT_MASK)
463 const struct LocalConf* conf_get_local(void)
469 * attach_confs_byname
471 * Attach a CONF line to a client if the name passed matches that for
472 * the conf file (for non-C/N lines) or is an exact match (C/N lines
473 * only). The difference in behaviour is to stop C:*::* and N:*::*.
475 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
478 struct ConfItem* tmp;
479 struct ConfItem* first = NULL;
483 if (HOSTLEN < strlen(name))
486 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
487 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
488 assert(0 != tmp->name);
489 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
490 if (ACR_OK == attach_conf(cptr, tmp) && !first)
499 * Added for new access check meLazy
501 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
504 struct ConfItem* tmp;
505 struct ConfItem* first = 0;
508 if (HOSTLEN < strlen(host))
511 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
512 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
513 assert(0 != tmp->host);
514 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
515 if (ACR_OK == attach_conf(cptr, tmp) && !first)
524 * find a conf entry which matches the hostname and has the same name.
526 struct ConfItem* find_conf_exact(const char* name, const char* user,
527 const char* host, int statmask)
529 struct ConfItem *tmp;
530 char userhost[USERLEN + HOSTLEN + 3];
533 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
535 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
537 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
538 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
539 0 != ircd_strcmp(tmp->name, name))
542 * Accept if the *real* hostname (usually sockecthost)
543 * socket host) matches *either* host or name field
544 * of the configuration.
546 if (match(tmp->host, userhost))
548 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
549 if (tmp->clients < MaxLinks(tmp->conn_class))
560 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
563 struct ConfItem* tmp;
566 if (HOSTLEN < strlen(name))
569 for (; lp; lp = lp->next) {
570 tmp = lp->value.aconf;
571 if (0 != (tmp->status & statmask)) {
572 assert(0 != tmp->name);
573 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
581 * Added for new access check meLazy
583 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
586 struct ConfItem* tmp = NULL;
589 if (HOSTLEN < strlen(host))
592 for (; lp; lp = lp->next) {
593 tmp = lp->value.aconf;
594 if (0 != (tmp->status & statmask)) {
595 assert(0 != tmp->host);
596 if (0 == match(tmp->host, host))
606 * Find a conf line using the IP# stored in it to search upon.
607 * Added 1/8/92 by Avalon.
609 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
612 struct ConfItem* tmp;
614 for (; lp; lp = lp->next) {
615 tmp = lp->value.aconf;
616 if (0 != (tmp->status & statmask)) {
617 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
627 * - looks for a match on all given fields.
629 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
632 struct ConfItem *bconf;
635 mask &= ~CONF_ILLEGAL;
637 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
638 if (!(bconf->status & mask) || (bconf->port != aconf->port))
641 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
642 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
644 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
647 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
648 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
650 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
652 && 0 != ircd_strcmp(bconf->passwd, "ONE")
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))
669 * If conf line is a class definition, create a class entry
670 * for it and make the conf_line illegal and delete it.
672 void conf_add_class(const char* const* fields, int count)
676 add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
677 atoi(fields[4]), atoi(fields[5]));
680 void conf_add_listener(const char* const* fields, int count)
688 if (count < 5 || EmptyString(fields[4]))
691 if (!EmptyString(fields[3])) {
692 const char* x = fields[3];
693 if ('S' == ToUpper(*x))
696 if ('H' == ToUpper(*x))
699 /* port vhost mask */
700 add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
703 void conf_add_local(const char* const* fields, int count)
705 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[5])) {
706 log_write(LS_CONFIG, L_CRIT, 0, "Your M: line must have 6 fields!");
710 * these two can only be set the first time
712 if (0 == localConf.name) {
713 if (string_is_hostname(fields[1]))
714 DupString(localConf.name, fields[1]);
716 if (0 == localConf.numeric) {
717 localConf.numeric = atoi(fields[5]);
718 if (0 == localConf.numeric)
719 log_write(LS_CONFIG, L_WARNING, 0,
720 "Your M: line must have a Numeric value greater than 0");
723 * these two can be changed while the server is running
725 if (string_is_address(fields[2])) {
726 if (INADDR_NONE == (localConf.vhost_address.s_addr = inet_addr(fields[2])))
727 localConf.vhost_address.s_addr = INADDR_ANY;
729 MyFree(localConf.description);
730 DupString(localConf.description, fields[3]);
732 * XXX - shouldn't be setting these directly here
734 ircd_strncpy(cli_info(&me), fields[3], REALLEN);
735 set_virtual_host(localConf.vhost_address);
738 void conf_add_admin(const char* const* fields, int count)
741 * if you have one, it MUST have 3 lines
744 log_write(LS_CONFIG, L_CRIT, 0, "Your A: line must have 4 fields!");
747 MyFree(localConf.location1);
748 DupString(localConf.location1, fields[1]);
750 MyFree(localConf.location2);
751 DupString(localConf.location2, fields[2]);
753 MyFree(localConf.contact);
754 DupString(localConf.contact, fields[3]);
758 * conf_add_crule - Create expression tree from connect rule and add it
761 void conf_add_crule(const char* const* fields, int count, int type)
763 struct CRuleNode* node;
766 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
769 if ((node = crule_parse(fields[3]))) {
770 struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
773 DupString(p->hostmask, fields[1]);
774 collapse(p->hostmask);
776 DupString(p->rule, fields[3]);
780 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;
805 void conf_add_server(const char* const* fields, int count)
807 struct ServerConf* server;
811 * missing host, password, or alias?
813 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
818 if (string_is_hostname(fields[1]))
819 addr.s_addr = INADDR_NONE;
820 else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
823 server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
825 DupString(server->hostname, fields[1]);
826 DupString(server->passwd, fields[2]);
827 DupString(server->alias, fields[3]);
828 server->address.s_addr = addr.s_addr;
829 server->port = atoi(fields[4]);
830 server->dns_pending = 0;
831 server->connected = 0;
833 server->conn_class = find_class(atoi(fields[5]));
835 server->next = serverConfList;
836 serverConfList = server;
838 // if (INADDR_NONE == server->address.s_addr)
839 // lookup_confhost(server);
842 void conf_add_deny(const char* const* fields, int count, int ip_kill)
844 struct DenyConf* conf;
846 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
849 conf = (struct DenyConf*) MyMalloc(sizeof(struct DenyConf));
851 memset(conf, 0, sizeof(struct DenyConf));
853 DupString(conf->hostmask, fields[1]);
854 collapse(conf->hostmask);
856 if (!EmptyString(fields[2])) {
857 const char* p = fields[2];
862 DupString(conf->message, p);
864 DupString(conf->usermask, fields[3]);
865 collapse(conf->usermask);
869 * Here we use the same kludge as in listener.c to parse
870 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
876 c_class = sscanf(conf->hostmask, "%d.%d.%d.%d/%d",
877 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
879 conf->bits = c_class * 8;
884 sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
887 * This ensures endian correctness
889 conf->s_addr = inet_addr(ipname);
890 Debug((DEBUG_DEBUG, "IPkill: %s = %08x/%i (%08x)", ipname,
891 conf->s_addr, conf->bits, NETMASK(conf->bits)));
893 conf->next = denyConfList;
897 void conf_erase_deny_list(void)
899 struct DenyConf* next;
900 struct DenyConf* p = denyConfList;
901 for ( ; p; p = next) {
911 const struct DenyConf* conf_get_deny_list(void)
917 * read_configuration_file
919 * Read configuration file.
921 * returns 0, if file cannot be opened
925 #define MAXCONFLINKS 150
927 int read_configuration_file(void)
929 enum { MAX_FIELDS = 15 };
937 struct ConfItem *aconf = 0;
940 const char* field_vector[MAX_FIELDS + 1];
942 Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
943 if (0 == (file = fbopen(configfile, "r"))) {
947 while (fbgets(line, sizeof(line) - 1, file)) {
948 if ('#' == *line || IsSpace(*line))
951 if ((src = strchr(line, '\n')))
954 if (':' != line[1]) {
955 Debug((DEBUG_ERROR, "Bad config line: %s", line));
956 sendto_opmask_butone(0, SNO_OLDSNO,"Bad Config line");
961 * do escapes, quoting, comments, and field breakup in place
962 * in one pass with a poor mans state machine
964 field_vector[0] = line;
968 for (src = line, dest = line; *src; ) {
1023 field_vector[field_count++] = dest;
1024 if (field_count > MAX_FIELDS)
1040 if (field_count < 2 || EmptyString(field_vector[0]))
1046 aconf = make_conf();
1048 switch (*field_vector[0]) {
1049 case 'A': /* Name, e-mail address of administrator */
1050 case 'a': /* of this server. CONF_ADMIN */
1051 conf_add_admin(field_vector, field_count);
1052 aconf->status = CONF_ILLEGAL;
1054 case 'C': /* Server where I should try to connect */
1055 case 'c': /* in case of lp failures */
1057 aconf->status = CONF_SERVER;
1060 case 'D': /* CONF_CRULEALL */
1061 conf_add_crule(field_vector, field_count, CRULE_ALL);
1062 aconf->status = CONF_ILLEGAL;
1064 /* Connect rule - autos only */
1065 case 'd': /* CONF_CRULEAUTO */
1066 conf_add_crule(field_vector, field_count, CRULE_AUTO);
1067 aconf->status = CONF_ILLEGAL;
1069 case 'F': /* Feature line */
1071 feature_set(0, &field_vector[1], field_count - 1);
1072 aconf->status = CONF_ILLEGAL;
1074 case 'H': /* Hub server line */
1076 aconf->status = CONF_HUB;
1078 case 'I': /* Just plain normal irc client trying */
1079 case 'i': /* to connect me */
1080 aconf->status = CONF_CLIENT;
1082 case 'K': /* Kill user line on irc.conf */
1083 conf_add_deny(field_vector, field_count, 0);
1084 aconf->status = CONF_ILLEGAL;
1086 case 'k': /* Kill user line based on IP in ircd.conf */
1087 conf_add_deny(field_vector, field_count, 1);
1088 aconf->status = CONF_ILLEGAL;
1090 /* Operator. Line should contain at least */
1091 /* password and host where connection is */
1092 case 'L': /* guaranteed leaf server */
1094 aconf->status = CONF_LEAF;
1096 /* Me. Host field is name used for this host */
1097 /* and port number is the number of the port */
1099 case 'm': /* CONF_ME */
1100 conf_add_local(field_vector, field_count);
1101 aconf->status = CONF_ILLEGAL;
1104 aconf->status = CONF_OPERATOR;
1106 /* Local Operator, (limited privs --SRB) */
1108 aconf->status = CONF_LOCOP;
1110 case 'P': /* listen port line */
1111 case 'p': /* CONF_LISTEN_PORT */
1112 conf_add_listener(field_vector, field_count);
1113 aconf->status = CONF_ILLEGAL;
1115 case 'T': /* print out different motd's */
1116 case 't': /* based on hostmask - CONF_TLINES */
1117 motd_add(field_vector[1], field_vector[2]);
1118 aconf->status = CONF_ILLEGAL;
1120 case 'U': /* Underworld server, allowed to hack modes */
1121 case 'u': /* *Every* server on the net must define the same !!! */
1122 aconf->status = CONF_UWORLD;
1125 case 'y': /* CONF_CLASS */
1126 conf_add_class(field_vector, field_count);
1127 aconf->status = CONF_ILLEGAL;
1130 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1131 sendto_opmask_butone(0, SNO_OLDSNO, "Unknown prefix in config file: %c",
1133 aconf->status = CONF_ILLEGAL;
1136 if (IsIllegal(aconf))
1139 if (!EmptyString(field_vector[1]))
1140 DupString(aconf->host, field_vector[1]);
1142 if (!EmptyString(field_vector[2]))
1143 DupString(aconf->passwd, field_vector[2]);
1145 if (field_count > 3 && !EmptyString(field_vector[3]))
1146 DupString(aconf->name, field_vector[3]);
1148 if (field_count > 4 && !EmptyString(field_vector[4]))
1149 aconf->port = atoi(field_vector[4]);
1151 if (field_count > 5 && !EmptyString(field_vector[5]))
1152 aconf->conn_class = find_class(atoi(field_vector[5]));
1155 * Associate each conf line with a class by using a pointer
1156 * to the correct class record. -avalon
1158 if (aconf->status & CONF_CLIENT_MASK) {
1159 if (aconf->conn_class == 0)
1160 aconf->conn_class = find_class(0);
1162 if (aconf->status & CONF_CLIENT) {
1163 struct ConfItem *bconf;
1165 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1167 bconf->status &= ~CONF_ILLEGAL;
1168 if (aconf->status == CONF_CLIENT) {
1170 * copy the password field in case it changed
1172 MyFree(bconf->passwd);
1173 bconf->passwd = aconf->passwd;
1176 ConfLinks(bconf) -= bconf->clients;
1177 bconf->conn_class = aconf->conn_class;
1178 if (bconf->conn_class)
1179 ConfLinks(bconf) += bconf->clients;
1185 if (aconf->status & CONF_SERVER) {
1186 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1187 strchr(aconf->host, '?') || !aconf->name)
1190 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1191 if (!strchr(aconf->host, '@')) {
1193 int len = 3; /* *@\0 = 3 */
1195 len += strlen(aconf->host);
1196 newhost = (char*) MyMalloc(len);
1197 assert(0 != newhost);
1198 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1199 MyFree(aconf->host);
1200 aconf->host = newhost;
1203 if (aconf->status & CONF_SERVER) {
1204 if (EmptyString(aconf->passwd))
1206 lookup_confhost(aconf);
1209 * Juped nicks are listed in the 'password' field of U:lines,
1210 * the list is comma separated and might be empty and/or contain
1211 * empty elements... the only limit is that it MUST be shorter
1212 * than 512 chars, or they will be cutted out :)
1214 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1215 addNickJupes(aconf->passwd);
1217 collapse(aconf->host);
1218 collapse(aconf->name);
1219 Debug((DEBUG_NOTICE,
1220 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1221 aconf->status, aconf->host, aconf->passwd,
1222 aconf->name, aconf->port, aconf->conn_class));
1223 aconf->next = GlobalConfList;
1224 GlobalConfList = aconf;
1230 nextping = nextconnect = CurrentTime;
1237 * Actual REHASH service routine. Called with sig == 0 if it has been called
1238 * as a result of an operator issuing this command, else assume it has been
1239 * called as a result of the server receiving a HUP signal.
1241 int rehash(struct Client *cptr, int sig)
1243 struct ConfItem** tmp = &GlobalConfList;
1244 struct ConfItem* tmp2;
1245 struct Client* acptr;
1251 sendto_opmask_butone(0, SNO_OLDSNO,
1252 "Got signal SIGHUP, reloading ircd conf. file");
1254 while ((tmp2 = *tmp)) {
1255 if (tmp2->clients) {
1257 * Configuration entry is still in use by some
1258 * local clients, cannot delete it--mark it so
1259 * that it will be deleted when the last client
1262 if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1268 tmp2->status |= CONF_ILLEGAL;
1275 conf_erase_crule_list();
1276 conf_erase_deny_list();
1280 * delete the juped nicks list
1285 flush_resolver_cache();
1287 class_mark_delete();
1288 mark_listeners_closing();
1290 read_configuration_file();
1292 log_reopen(); /* reopen log files */
1295 class_delete_marked(); /* unless it fails */
1298 * Flush out deleted I and P lines although still in use.
1300 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1301 if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1310 for (i = 0; i <= HighestFd; i++) {
1311 if ((acptr = LocalClientArray[i])) {
1312 assert(!IsMe(acptr));
1313 if (IsServer(acptr)) {
1314 det_confs_butmask(acptr,
1315 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1316 attach_confs_byname(acptr, cli_name(acptr),
1317 CONF_HUB | CONF_LEAF | CONF_UWORLD);
1319 /* Because admin's are getting so uppity about people managing to
1320 * get past K/G's etc, we'll "fix" the bug by actually explaining
1323 if ((found_g = find_kill(acptr))) {
1324 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1325 found_g == -2 ? "G-line active for %s%s" :
1326 "K-line active for %s%s",
1327 IsUnknown(acptr) ? "Unregistered Client ":"",
1328 get_client_name(acptr, HIDE_IP));
1329 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1330 "K-lined") == CPTR_KILLED)
1341 * Read configuration file.
1343 * returns 0, if file cannot be opened
1349 if (read_configuration_file()) {
1351 * make sure we're sane to start if the config
1352 * file read didn't get everything we need.
1353 * XXX - should any of these abort the server?
1354 * TODO: add warning messages
1356 if (0 == localConf.name || 0 == localConf.numeric)
1359 if (0 == localConf.location1)
1360 DupString(localConf.location1, "");
1361 if (0 == localConf.location2)
1362 DupString(localConf.location2, "");
1363 if (0 == localConf.contact)
1364 DupString(localConf.contact, "");
1376 * 0: Client may continue to try and connect
1377 * -1: Client was K/k:'d - sideeffect: reason was sent.
1378 * -2: Client was G/g:'d - sideeffect: reason was sent.
1380 * Client may have been sent a reason why they are denied, as above.
1382 int find_kill(struct Client *cptr)
1386 struct DenyConf* deny;
1387 struct Gline* agline = NULL;
1391 if (!cli_user(cptr))
1394 host = cli_sockhost(cptr);
1395 name = cli_user(cptr)->username;
1397 assert(strlen(host) <= HOSTLEN);
1398 assert((name ? strlen(name) : 0) <= HOSTLEN);
1400 /* 2000-07-14: Rewrote this loop for massive speed increases.
1403 for (deny = denyConfList; deny; deny = deny->next) {
1404 if (0 != match(deny->usermask, name))
1407 if (EmptyString(deny->hostmask))
1410 if (deny->ip_kill) { /* k: by IP */
1411 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1412 cli_ip(cptr).s_addr, deny->s_addr, deny->bits, NETMASK(deny->bits)));
1413 if ((cli_ip(cptr).s_addr & NETMASK(deny->bits)) == deny->s_addr)
1416 else if (0 == match(deny->hostmask, host))
1420 if (EmptyString(deny->message))
1421 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1422 ":Connection from your host is refused on this server.");
1425 killcomment(cptr, deny->message);
1427 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
1430 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline)) {
1432 * find active glines
1433 * added a check against the user's IP address to find_gline() -Kev
1435 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
1438 agline = 0; /* if a gline was found, it was inactive */
1449 * Ordinary client access check. Look for conf lines which have the same
1450 * status as the flags passed.
1452 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1454 enum AuthorizationCheckResult acr = ACR_OK;
1458 if ((acr = attach_iline(cptr))) {
1459 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1460 cli_name(cptr), cli_sockhost(cptr)));
1469 * Check access for a server given its name (passed in cptr struct).
1470 * Must check for all C/N lines which have a name which matches the
1471 * name given and a host which matches. A host alias which is the
1472 * same as the server name is also acceptable in the host field of a
1477 * -1 = Access denied
1480 int conf_check_server(struct Client *cptr)
1482 struct ConfItem* c_conf = NULL;
1485 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1486 cli_name(cptr), cli_sockhost(cptr)));
1488 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cli_name(cptr), CONF_SERVER)) {
1489 Debug((DEBUG_DNS, "No C/N lines for %s", cli_sockhost(cptr)));
1492 lp = cli_confs(cptr);
1494 * We initiated this connection so the client should have a C and N
1495 * line already attached after passing through the connect_server()
1498 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1499 c_conf = find_conf_byname(lp, cli_name(cptr), CONF_SERVER);
1501 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1503 det_confs_butmask(cptr, 0);
1511 if (cli_dns_reply(cptr)) {
1513 struct hostent* hp = cli_dns_reply(cptr)->hp;
1514 const char* name = hp->h_name;
1516 * If we are missing a C or N line from above, search for
1517 * it under all known hostnames we have for this ip#.
1519 for (i = 0; name; name = hp->h_aliases[i++]) {
1520 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1521 ircd_strncpy(cli_sockhost(cptr), name, HOSTLEN);
1526 for (i = 0; hp->h_addr_list[i]; i++) {
1527 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1534 * Check for C lines with the hostname portion the ip number
1535 * of the host the server runs on. This also checks the case where
1536 * there is a server connecting from 'localhost'.
1538 c_conf = find_conf_byhost(lp, cli_sockhost(cptr), CONF_SERVER);
1542 * Attach by IP# only if all other checks have failed.
1543 * It is quite possible to get here with the strange things that can
1544 * happen when using DNS in the way the irc server does. -avalon
1547 c_conf = find_conf_byip(lp, (const char*) &(cli_ip(cptr)), CONF_SERVER);
1549 * detach all conf lines that got attached by attach_confs()
1551 det_confs_butmask(cptr, 0);
1553 * if no C or no N lines, then deny access
1556 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1557 cli_name(cptr), cli_username(cptr), cli_sockhost(cptr)));
1560 ircd_strncpy(cli_name(cptr), c_conf->name, HOSTLEN);
1562 * attach the C and N lines to the client structure for later use.
1564 attach_conf(cptr, c_conf);
1565 attach_confs_byname(cptr, cli_name(cptr), CONF_HUB | CONF_LEAF | CONF_UWORLD);
1567 if (INADDR_NONE == c_conf->ipnum.s_addr)
1568 c_conf->ipnum.s_addr = cli_ip(cptr).s_addr;
1570 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cli_name(cptr), cli_sockhost(cptr)));