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)
475 struct ConfItem* find_me(void)
477 struct ConfItem* aconf;
478 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
479 if (aconf->status & CONF_ME)
486 * attach_confs_byname
488 * Attach a CONF line to a client if the name passed matches that for
489 * the conf file (for non-C/N lines) or is an exact match (C/N lines
490 * only). The difference in behaviour is to stop C:*::* and N:*::*.
492 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
495 struct ConfItem* tmp;
496 struct ConfItem* first = NULL;
500 if (HOSTLEN < strlen(name))
503 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
504 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
505 assert(0 != tmp->name);
506 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
507 if (ACR_OK == attach_conf(cptr, tmp) && !first)
516 * Added for new access check meLazy
518 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
521 struct ConfItem* tmp;
522 struct ConfItem* first = 0;
525 if (HOSTLEN < strlen(host))
528 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
529 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
530 assert(0 != tmp->host);
531 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
532 if (ACR_OK == attach_conf(cptr, tmp) && !first)
541 * find a conf entry which matches the hostname and has the same name.
543 struct ConfItem* find_conf_exact(const char* name, const char* user,
544 const char* host, int statmask)
546 struct ConfItem *tmp;
547 char userhost[USERLEN + HOSTLEN + 3];
550 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
552 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
554 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
555 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
556 0 != ircd_strcmp(tmp->name, name))
559 * Accept if the *real* hostname (usually sockecthost)
560 * socket host) matches *either* host or name field
561 * of the configuration.
563 if (match(tmp->host, userhost))
565 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
566 if (tmp->clients < MaxLinks(tmp->confClass))
577 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
580 struct ConfItem* tmp;
583 if (HOSTLEN < strlen(name))
586 for (; lp; lp = lp->next) {
587 tmp = lp->value.aconf;
588 if (0 != (tmp->status & statmask)) {
589 assert(0 != tmp->name);
590 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
598 * Added for new access check meLazy
600 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
603 struct ConfItem* tmp = NULL;
606 if (HOSTLEN < strlen(host))
609 for (; lp; lp = lp->next) {
610 tmp = lp->value.aconf;
611 if (0 != (tmp->status & statmask)) {
612 assert(0 != tmp->host);
613 if (0 == match(tmp->host, host))
623 * Find a conf line using the IP# stored in it to search upon.
624 * Added 1/8/92 by Avalon.
626 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
629 struct ConfItem* tmp;
631 for (; lp; lp = lp->next) {
632 tmp = lp->value.aconf;
633 if (0 != (tmp->status & statmask)) {
634 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
644 * - looks for a match on all given fields.
646 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
649 struct ConfItem *bconf;
652 mask &= ~CONF_ILLEGAL;
654 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
655 if (!(bconf->status & mask) || (bconf->port != aconf->port))
658 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
659 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
661 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
664 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
665 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
667 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
669 && 0 != ircd_strcmp(bconf->passwd, "ONE")
671 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
674 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
675 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
677 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
686 * If conf line is a class definition, create a class entry
687 * for it and make the conf_line illegal and delete it.
689 void conf_add_class(const char* const* fields, int count)
693 add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
694 atoi(fields[4]), atoi(fields[5]));
697 void conf_add_listener(const char* const* fields, int count)
705 if (count < 5 || EmptyString(fields[4]))
708 if (!EmptyString(fields[3])) {
709 const char* x = fields[3];
710 if ('S' == ToUpper(*x))
713 if ('H' == ToUpper(*x))
716 /* port vhost mask */
717 add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
720 void conf_add_admin(const char* const* fields, int count)
723 * if you have one, it MUST have 3 lines
726 Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
727 ircd_log(L_CRIT, "Your A: line must have 4 fields!\n");
730 MyFree(localConf.location1);
731 DupString(localConf.location1, fields[1]);
733 MyFree(localConf.location2);
734 DupString(localConf.location2, fields[2]);
736 MyFree(localConf.contact);
737 DupString(localConf.contact, fields[3]);
740 void conf_add_motd(const char* const* fields, int count, struct MotdConf** list)
742 struct MotdConf* conf;
743 if (count < 3 || EmptyString(fields[1]) || EmptyString(fields[2]))
746 conf = (struct MotdConf*) MyMalloc(sizeof(struct MotdConf));
749 DupString(conf->hostmask, fields[1]);
750 collapse(conf->hostmask);
752 DupString(conf->path, fields[2]);
760 void conf_erase_motd_list(struct MotdConf** list)
763 struct MotdConf* next;
767 for (p = *list; p; p = next) {
776 const struct MotdConf* conf_get_motd_list(void)
782 * conf_add_crule - Create expression tree from connect rule and add it
785 void conf_add_crule(const char* const* fields, int count, int type)
787 struct CRuleNode* node;
790 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
793 if ((node = crule_parse(fields[3]))) {
794 struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
797 DupString(p->hostmask, fields[1]);
798 collapse(p->hostmask);
800 DupString(p->rule, fields[3]);
804 p->next = cruleConfList;
809 void conf_erase_crule_list(void)
811 struct CRuleConf* next;
812 struct CRuleConf* p = cruleConfList;
814 for ( ; p; p = next) {
816 crule_free(&p->node);
824 const struct CRuleConf* conf_get_crule_list(void)
826 return cruleConfList;
829 void conf_add_server(const char* const* fields, int count)
831 struct ServerConf* server;
835 * missing host, password, or alias?
837 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
842 if (string_is_hostname(fields[1]))
843 addr.s_addr = INADDR_NONE;
844 else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
847 server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
849 DupString(server->hostname, fields[1]);
850 DupString(server->passwd, fields[2]);
851 DupString(server->alias, fields[3]);
852 server->address.s_addr = addr.s_addr;
853 server->port = atoi(fields[4]);
854 server->dns_pending = 0;
855 server->connected = 0;
857 server->confClass = find_class(atoi(fields[5]));
859 server->next = serverConfList;
860 serverConfList = server;
862 // if (INADDR_NONE == server->address.s_addr)
863 // lookup_confhost(server);
868 * read_configuration_file
870 * Read configuration file.
872 * returns 0, if file cannot be opened
876 #define MAXCONFLINKS 150
878 int read_configuration_file(void)
880 enum { MAX_FIELDS = 15 };
888 struct ConfItem *aconf = 0;
891 const char* field_vector[MAX_FIELDS + 1];
893 Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
894 if (0 == (file = fbopen(configfile, "r"))) {
898 while (fbgets(line, sizeof(line) - 1, file)) {
899 if ('#' == *line || IsSpace(*line))
902 if ((src = strchr(line, '\n')))
905 if (':' != line[1]) {
906 Debug((DEBUG_ERROR, "Bad config line: %s", line));
907 sendto_op_mask(SNO_OLDSNO,"Bad Config line");
912 * do escapes, quoting, comments, and field breakup in place
913 * in one pass with a poor mans state machine
915 field_vector[0] = line;
919 for (src = line, dest = line; *src; ) {
974 field_vector[field_count++] = dest;
975 if (field_count > MAX_FIELDS)
991 if (field_count < 2 || EmptyString(field_vector[0]))
999 switch (*field_vector[0]) {
1000 case 'A': /* Name, e-mail address of administrator */
1001 case 'a': /* of this server. CONF_ADMIN */
1002 conf_add_admin(field_vector, field_count);
1003 aconf->status = CONF_ILLEGAL;
1005 case 'C': /* Server where I should try to connect */
1006 case 'c': /* in case of lp failures */
1008 aconf->status = CONF_SERVER;
1011 case 'D': /* CONF_CRULEALL */
1012 conf_add_crule(field_vector, field_count, CRULE_ALL);
1013 aconf->status = CONF_ILLEGAL;
1015 /* Connect rule - autos only */
1016 case 'd': /* CONF_CRULEAUTO */
1017 conf_add_crule(field_vector, field_count, CRULE_AUTO);
1018 aconf->status = CONF_ILLEGAL;
1020 case 'H': /* Hub server line */
1022 aconf->status = CONF_HUB;
1024 case 'I': /* Just plain normal irc client trying */
1025 case 'i': /* to connect me */
1026 aconf->status = CONF_CLIENT;
1028 case 'K': /* Kill user line on irc.conf */
1029 aconf->status = CONF_KILL;
1031 case 'k': /* Kill user line based on IP in ircd.conf */
1032 aconf->status = CONF_IPKILL;
1034 /* Operator. Line should contain at least */
1035 /* password and host where connection is */
1036 case 'L': /* guaranteed leaf server */
1038 aconf->status = CONF_LEAF;
1040 /* Me. Host field is name used for this host */
1041 /* and port number is the number of the port */
1044 aconf->status = CONF_ME;
1047 aconf->status = CONF_OPERATOR;
1049 /* Local Operator, (limited privs --SRB) */
1051 aconf->status = CONF_LOCOP;
1053 case 'P': /* listen port line */
1054 case 'p': /* CONF_LISTEN_PORT */
1055 conf_add_listener(field_vector, field_count);
1056 aconf->status = CONF_ILLEGAL;
1058 case 'T': /* print out different motd's */
1059 case 't': /* based on hostmask - CONF_TLINES */
1060 conf_add_motd(field_vector, field_count, &motdConfList);
1061 aconf->status = CONF_ILLEGAL;
1063 case 'U': /* Underworld server, allowed to hack modes */
1064 case 'u': /* *Every* server on the net must define the same !!! */
1065 aconf->status = CONF_UWORLD;
1068 case 'y': /* CONF_CLASS */
1069 conf_add_class(field_vector, field_count);
1070 aconf->status = CONF_ILLEGAL;
1073 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1074 sendto_op_mask(SNO_OLDSNO,"Unknown prefix in config file: %c", *field_vector[0]);
1075 aconf->status = CONF_ILLEGAL;
1078 if (IsIllegal(aconf))
1081 if (!EmptyString(field_vector[1]))
1082 DupString(aconf->host, field_vector[1]);
1084 if (!EmptyString(field_vector[2]))
1085 DupString(aconf->passwd, field_vector[2]);
1087 if (field_count > 3 && !EmptyString(field_vector[3]))
1088 DupString(aconf->name, field_vector[3]);
1090 if (field_count > 4 && !EmptyString(field_vector[4]))
1091 aconf->port = atoi(field_vector[4]);
1093 if (field_count > 5 && !EmptyString(field_vector[5])) {
1094 int n = atoi(field_vector[5]);
1095 if (CONF_ME == (aconf->status & CONF_ME))
1096 SetYXXServerName(&me, n); /* Our Numeric Nick */
1098 aconf->confClass = find_class(n);
1100 else if (CONF_ME == (aconf->status & CONF_ME)) {
1101 Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1102 "assigned to you by routing-com!\n"));
1103 ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1104 "assigned to you by routing-com!\n");
1108 * Associate each conf line with a class by using a pointer
1109 * to the correct class record. -avalon
1111 if (aconf->status & CONF_CLIENT_MASK) {
1112 if (aconf->confClass == 0)
1113 aconf->confClass = find_class(0);
1115 if (aconf->status & CONF_CLIENT) {
1116 struct ConfItem *bconf;
1118 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1120 bconf->status &= ~CONF_ILLEGAL;
1121 if (aconf->status == CONF_CLIENT) {
1123 * copy the password field in case it changed
1125 MyFree(bconf->passwd);
1126 bconf->passwd = aconf->passwd;
1129 ConfLinks(bconf) -= bconf->clients;
1130 bconf->confClass = aconf->confClass;
1131 if (bconf->confClass)
1132 ConfLinks(bconf) += bconf->clients;
1138 if (aconf->status & CONF_SERVER) {
1139 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1140 strchr(aconf->host, '?') || !aconf->name)
1143 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1144 if (!strchr(aconf->host, '@')) {
1146 int len = 3; /* *@\0 = 3 */
1148 len += strlen(aconf->host);
1149 newhost = (char*) MyMalloc(len);
1150 assert(0 != newhost);
1151 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1152 MyFree(aconf->host);
1153 aconf->host = newhost;
1156 if (aconf->status & CONF_SERVER) {
1157 if (EmptyString(aconf->passwd))
1159 lookup_confhost(aconf);
1162 * Own port and name cannot be changed after the startup.
1163 * (or could be allowed, but only if all links are closed first).
1164 * Configuration info does not override the name and port
1165 * if previously defined. Note, that "info"-field can be
1166 * changed by "/rehash".
1168 if (aconf->status == CONF_ME) {
1169 ircd_strncpy(me.info, aconf->name, REALLEN);
1172 if (aconf->status == CONF_IPKILL) {
1174 * Here we use the same kludge as in listener.c to parse
1175 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
1181 class=sscanf(aconf->host,"%d.%d.%d.%d/%d",
1182 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
1184 aconf->bits=class*8;
1189 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0], ad[1], ad[2], ad[3]);
1191 /* This ensures endian correctness */
1192 aconf->ipnum.s_addr = inet_addr(ipname);
1193 Debug((DEBUG_DEBUG,"IPkill: %s = %08x/%i (%08x)",ipname,
1194 aconf->ipnum.s_addr,aconf->bits,NETMASK(aconf->bits)));
1198 * Juped nicks are listed in the 'password' field of U:lines,
1199 * the list is comma separated and might be empty and/or contain
1200 * empty elements... the only limit is that it MUST be shorter
1201 * than 512 chars, or they will be cutted out :)
1203 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1204 addNickJupes(aconf->passwd);
1206 collapse(aconf->host);
1207 collapse(aconf->name);
1208 Debug((DEBUG_NOTICE,
1209 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1210 aconf->status, aconf->host, aconf->passwd,
1211 aconf->name, aconf->port, aconf->confClass));
1212 aconf->next = GlobalConfList;
1213 GlobalConfList = aconf;
1220 nextping = nextconnect = CurrentTime;
1227 * Actual REHASH service routine. Called with sig == 0 if it has been called
1228 * as a result of an operator issuing this command, else assume it has been
1229 * called as a result of the server receiving a HUP signal.
1231 int rehash(struct Client *cptr, int sig)
1233 struct ConfItem** tmp = &GlobalConfList;
1234 struct ConfItem* tmp2;
1235 struct ConfClass* cltmp;
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 * We don't delete the class table, rather mark all entries
1272 * for deletion. The table is cleaned up by check_class(). - avalon
1274 for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
1278 * delete the juped nicks list
1283 flush_resolver_cache();
1285 mark_listeners_closing();
1287 if (!read_configuration_file()) /* This calls check_class(), */
1288 check_class(); /* unless it fails */
1293 * Flush out deleted I and P lines although still in use.
1295 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1296 if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1305 for (i = 0; i <= HighestFd; i++) {
1306 if ((acptr = LocalClientArray[i])) {
1307 assert(!IsMe(acptr));
1308 if (IsServer(acptr)) {
1309 det_confs_butmask(acptr,
1310 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1311 attach_confs_byname(acptr, acptr->name,
1312 CONF_HUB | CONF_LEAF | CONF_UWORLD);
1314 /* Because admin's are getting so uppity about people managing to
1315 * get past K/G's etc, we'll "fix" the bug by actually explaining
1318 if ((found_g = find_kill(acptr))) {
1319 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1320 found_g == -2 ? "G-line active for %s%s" :
1321 "K-line active for %s%s",
1322 IsUnknown(acptr) ? "Unregistered Client ":"",
1323 get_client_name(acptr, HIDE_IP));
1324 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1325 "K-lined") == CPTR_KILLED)
1331 * free old motd structs
1343 /* reload motd files */
1345 rmotd = read_motd(RPATH);
1346 motd = read_motd(MPATH);
1353 * Read configuration file.
1355 * returns 0, if file cannot be opened
1361 if (read_configuration_file()) {
1363 * make sure we're sane to start if the config
1364 * file read didn't get everything we need.
1365 * XXX - should any of these abort the server?
1366 * TODO: add warning messages
1368 if (0 == localConf.location1)
1369 DupString(localConf.location1, "");
1370 if (0 == localConf.location2)
1371 DupString(localConf.location2, "");
1372 if (0 == localConf.contact)
1373 DupString(localConf.contact, "");
1382 * Read info from T:lines into TRecords which include the file
1383 * timestamp, the hostmask, and the contents of the motd file
1388 struct MotdConf* conf;
1389 struct TRecord *temp;
1390 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1391 struct MotdItem *amotd;
1393 /* Free the old trecords and the associated motd contents first */
1396 while (tdata->tmotd) {
1397 amotd = tdata->tmotd->next;
1398 MyFree(tdata->tmotd);
1399 tdata->tmotd = amotd;
1405 for (conf = motdConfList; conf; conf = conf->next) {
1406 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1409 temp->hostmask = conf->hostmask;
1410 temp->tmotd = read_motd(conf->path);
1411 temp->tmotd_tm = motd_tm;
1427 * 0: Client may continue to try and connect
1428 * -1: Client was K/k:'d - sideeffect: reason was sent.
1429 * -2: Client was G/g:'d - sideeffect: reason was sent.
1431 * Client may have been sent a reason why they are denied, as above.
1433 int find_kill(struct Client *cptr)
1437 struct ConfItem* tmp;
1438 struct Gline* agline = NULL;
1445 host = cptr->sockhost;
1446 name = cptr->user->username;
1448 assert(strlen(host) <= HOSTLEN);
1449 assert((name ? strlen(name) : 0) <= HOSTLEN);
1451 /* 2000-07-14: Rewrote this loop for massive speed increases.
1454 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1456 if ((tmp->status & CONF_KLINE) == 0)
1460 * What is this for? You can K: by port?!
1463 if (tmp->port && tmp->port != cptr->listener->port)
1466 if (!tmp->name || match(tmp->name, name) != 0)
1472 if (tmp->status != CONF_IPKILL) {
1473 if (match(tmp->host, host) == 0)
1476 else { /* k: by IP */
1477 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1478 cptr->ip.s_addr, tmp->ipnum.s_addr, tmp->bits,
1479 NETMASK(tmp->bits)));
1480 if ((cptr->ip.s_addr & NETMASK(tmp->bits)) == tmp->ipnum.s_addr)
1485 if (EmptyString(tmp->passwd))
1486 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1487 ":Connection from your host is refused on this server.");
1489 if (*tmp->passwd == '"') {
1490 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1491 strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1493 else if (*tmp->passwd == '!')
1494 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1496 #ifdef COMMENT_IS_FILE
1497 killcomment(cptr, cptr->name, tmp->passwd);
1499 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1505 /* find active glines */
1506 /* added a check against the user's IP address to find_gline() -Kev */
1507 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1508 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1509 GlineReason(agline));
1511 agline = NULL; /* if a gline was found, it was inactive */
1521 struct MotdItem* read_motd(const char* motdfile)
1523 FBFILE* file = NULL;
1524 struct MotdItem* temp;
1525 struct MotdItem* newmotd;
1526 struct MotdItem* last;
1531 if (NULL == (file = fbopen(motdfile, "r"))) {
1532 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1535 if (-1 == fbstat(&sb, file)) {
1539 newmotd = last = NULL;
1540 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1541 while (fbgets(line, sizeof(line) - 1, file)) {
1542 if ((tmp = (char *)strchr(line, '\n')))
1544 if ((tmp = (char *)strchr(line, '\r')))
1546 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1548 strcpy(temp->line, line);
1562 * Ordinary client access check. Look for conf lines which have the same
1563 * status as the flags passed.
1565 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1567 enum AuthorizationCheckResult acr = ACR_OK;
1571 if ((acr = attach_iline(cptr))) {
1572 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1573 cptr->name, cptr->sockhost));
1582 * Check access for a server given its name (passed in cptr struct).
1583 * Must check for all C/N lines which have a name which matches the
1584 * name given and a host which matches. A host alias which is the
1585 * same as the server name is also acceptable in the host field of a
1590 * -1 = Access denied
1593 int conf_check_server(struct Client *cptr)
1595 struct ConfItem* c_conf = NULL;
1598 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1599 cptr->name, cptr->sockhost));
1601 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1602 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1607 * We initiated this connection so the client should have a C and N
1608 * line already attached after passing through the connect_server()
1611 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1612 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1614 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1616 det_confs_butmask(cptr, 0);
1624 if (cptr->dns_reply) {
1626 struct hostent* hp = cptr->dns_reply->hp;
1627 const char* name = hp->h_name;
1629 * If we are missing a C or N line from above, search for
1630 * it under all known hostnames we have for this ip#.
1632 for (i = 0; name; name = hp->h_aliases[i++]) {
1633 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1634 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1639 for (i = 0; hp->h_addr_list[i]; i++) {
1640 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1647 * Check for C lines with the hostname portion the ip number
1648 * of the host the server runs on. This also checks the case where
1649 * there is a server connecting from 'localhost'.
1651 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1655 * Attach by IP# only if all other checks have failed.
1656 * It is quite possible to get here with the strange things that can
1657 * happen when using DNS in the way the irc server does. -avalon
1660 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1662 * detach all conf lines that got attached by attach_confs()
1664 det_confs_butmask(cptr, 0);
1666 * if no C or no N lines, then deny access
1669 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1670 cptr->name, cptr->username, cptr->sockhost));
1673 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1675 * attach the C and N lines to the client structure for later use.
1677 attach_conf(cptr, c_conf);
1678 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1680 if (INADDR_NONE == c_conf->ipnum.s_addr)
1681 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1683 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));