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;
79 * output the reason for being k lined from a file - Mmmm
81 * parv is the sender prefix
82 * filename is the file that is to be output to the K lined client
84 static void killcomment(struct Client *sptr, char *parv, 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 if ((tmp = strchr(line, '\n')))
103 if ((tmp = strchr(line, '\r')))
105 send_reply(sptr, RPL_MOTD, line);
107 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
108 ":Connection from your host is refused on this server.");
112 struct ConfItem* make_conf(void)
114 struct ConfItem* aconf;
116 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
121 aconf->status = CONF_ILLEGAL;
123 aconf->ipnum.s_addr = INADDR_NONE;
129 aconf->dns_pending = 0;
130 aconf->confClass = 0;
135 void delist_conf(struct ConfItem *aconf)
137 if (aconf == GlobalConfList)
138 GlobalConfList = GlobalConfList->next;
140 struct ConfItem *bconf;
142 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
144 bconf->next = aconf->next;
149 void free_conf(struct ConfItem *aconf)
151 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
152 aconf->host ? aconf->host : "*",
153 aconf->name ? aconf->name : "*",
155 if (aconf->dns_pending)
156 delete_resolver_queries(aconf);
159 memset(aconf->passwd, 0, strlen(aconf->passwd));
160 MyFree(aconf->passwd);
169 * conf_dns_callback - called when resolver query finishes
170 * if the query resulted in a successful search, hp will contain
171 * a non-null pointer, otherwise hp will be null.
172 * if successful save hp in the conf item it was called with
174 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
176 struct ConfItem* aconf = (struct ConfItem*) vptr;
177 aconf->dns_pending = 0;
179 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
183 * conf_dns_lookup - do a nameserver lookup of the conf host
184 * if the conf entry is currently doing a ns lookup do nothing, otherwise
185 * if the lookup returns a null pointer, set the conf dns_pending flag
187 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
189 struct DNSReply* dns_reply = 0;
190 if (!aconf->dns_pending) {
191 char buf[HOSTLEN + 1];
192 struct DNSQuery query;
194 query.callback = conf_dns_callback;
195 host_from_uh(buf, aconf->host, HOSTLEN);
198 if (0 == (dns_reply = gethost_byname(buf, &query)))
199 aconf->dns_pending = 1;
208 * Do (start) DNS lookups of all hostnames in the conf line and convert
209 * an IP addresses in a.b.c.d number for to IP#s.
211 static void lookup_confhost(struct ConfItem *aconf)
213 struct DNSReply* reply;
215 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
216 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
217 aconf->host, aconf->name));
221 * Do name lookup now on hostnames given and store the
222 * ip numbers in conf structure.
224 if (IsDigit(*aconf->host)) {
226 * rfc 1035 sez host names may not start with a digit
227 * XXX - this has changed code needs to be updated
229 aconf->ipnum.s_addr = inet_addr(aconf->host);
230 if (INADDR_NONE == aconf->ipnum.s_addr) {
231 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
232 aconf->host, aconf->name));
235 else if ((reply = conf_dns_lookup(aconf)))
236 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
240 * conf_find_server - find a server by name or hostname
241 * returns a server conf item pointer if found, 0 otherwise
243 * NOTE: at some point when we don't have to scan the entire
244 * list it may be cheaper to look for server names and host
245 * names in separate loops (original code did it that way)
247 struct ConfItem* conf_find_server(const char* name)
249 struct ConfItem* conf;
252 for (conf = GlobalConfList; conf; conf = conf->next) {
253 if (CONF_SERVER == conf->status) {
255 * Check first servernames, then try hostnames.
256 * XXX - match returns 0 if there _is_ a match... guess they
257 * haven't decided what true is yet
259 if (0 == match(name, conf->name))
267 * conf_eval_crule - evaluate connection rules
268 * returns the name of the rule triggered if found, 0 otherwise
270 * Evaluate connection rules... If no rules found, allow the
271 * connect. Otherwise stop with the first true rule (ie: rules
272 * are ored together. Oper connects are effected only by D
273 * lines (CRULEALL) not d lines (CRULEAUTO).
275 const char* conf_eval_crule(struct ConfItem* conf)
277 struct ConfItem* rule;
280 for (rule = GlobalConfList; rule; rule = rule->next) {
281 if ((CONF_CRULEALL == rule->status) && (0 == match(rule->host, conf->name))) {
282 if (crule_eval(rule->passwd))
291 * field breakup for ircd.conf file.
293 static char* getfield(char* newline, char fs)
295 static char* gfline = NULL;
305 end = field = gfline;
321 } while (end && fs != ':' && *++end != ':' && *end != '\n') ;
325 if ((end = strchr(field, '\n')) == NULL)
326 end = field + strlen(field);
337 * Remove all conf entries from the client except those which match
338 * the status field mask.
340 void det_confs_butmask(struct Client *cptr, int mask)
342 struct SLink *tmp, *tmp2;
344 for (tmp = cptr->confs; tmp; tmp = tmp2) {
346 if ((tmp->value.aconf->status & mask) == 0)
347 detach_conf(cptr, tmp->value.aconf);
352 * validate_hostent - make sure hostnames are valid in a hostent struct
353 * XXX - this is terrible, what's worse is it used to be in the inner
354 * loop of scanning all the I:lines --Bleep
356 static int validate_hostent(struct hostent* hp)
358 char fullname[HOSTLEN + 1];
363 for (hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
364 unsigned int fullnamelen = 0;
365 unsigned int label_count = 0;
367 ircd_strncpy(fullname, hname, HOSTLEN);
368 fullname[HOSTLEN] = '\0';
370 * Disallow a hostname label to contain anything but a [-a-zA-Z0-9].
371 * It may not start or end on a '.'.
372 * A label may not end on a '-', the maximum length of a label is
374 * On top of that (which seems to be the RFC) we demand that the
375 * top domain does not contain any digits.
377 error = (*hname == '.') ? 1 : 0; /* May not start with a '.' */
380 for (p = fullname; *p; ++p, ++fullnamelen) {
382 /* Label may not end on '-' and May not end on a '.' */
383 if (p[-1] == '-' || p[1] == 0) {
388 error = 0; /* Was not top domain */
391 if (++label_count > 63) {
392 /* Label not longer then 63 */
396 if (*p >= '0' && *p <= '9') {
397 /* In case this is top domain */
401 if (!(*p >= 'a' && *p <= 'z')
402 && !(*p >= 'A' && *p <= 'Z') && *p != '-') {
415 * check_limit_and_attach - check client limits and attach I:line
417 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
418 * and dislallow more than 255 people here as well as in ipcheck.
419 * removed the old "ONE" scheme too.
420 * -- Isomer 2000-06-22
422 static enum AuthorizationCheckResult
423 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
428 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
429 number = *aconf->passwd-'0';
430 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
432 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
434 if (ip_registry_count(cptr->ip.s_addr) > number)
435 return ACR_TOO_MANY_FROM_IP;
436 return attach_conf(cptr, aconf);
440 * Find the first (best) I line to attach.
442 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
444 struct ConfItem* aconf;
447 static char uhost[HOSTLEN + USERLEN + 3];
448 static char fullname[HOSTLEN + 1];
449 struct hostent* hp = 0;
451 if (cptr->dns_reply) {
452 hp = cptr->dns_reply->hp;
453 if (!validate_hostent(hp))
456 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
457 if (aconf->status != CONF_CLIENT)
459 if (aconf->port && aconf->port != cptr->listener->port)
461 if (!aconf->host || !aconf->name)
464 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
465 ircd_strncpy(fullname, hname, HOSTLEN);
466 fullname[HOSTLEN] = '\0';
468 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
470 if (strchr(aconf->name, '@')) {
471 strcpy(uhost, cptr->username);
476 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
477 uhost[sizeof(uhost) - 1] = 0;
478 if (0 == match(aconf->name, uhost)) {
479 if (strchr(uhost, '@'))
480 cptr->flags |= FLAGS_DOID;
481 return check_limit_and_attach(cptr, aconf);
485 if (strchr(aconf->host, '@')) {
486 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
487 uhost[sizeof(uhost) - 2] = 0;
492 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
493 uhost[sizeof(uhost) - 1] = 0;
494 if (match(aconf->host, uhost))
496 if (strchr(uhost, '@'))
497 cptr->flags |= FLAGS_DOID;
499 return check_limit_and_attach(cptr, aconf);
501 return ACR_NO_AUTHORIZATION;
505 * detach_conf - Disassociate configuration from the client.
507 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
514 assert(0 < aconf->clients);
519 if ((*lp)->value.aconf == aconf) {
520 if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) &&
521 ConfLinks(aconf) > 0)
523 if (0 == --aconf->clients && IsIllegal(aconf))
536 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
540 for (lp = cptr->confs; lp; lp = lp->next)
541 if (lp->value.aconf == aconf)
550 * Associate a specific configuration entry to a *local*
551 * client (this is the one which used in accepting the
552 * connection). Note, that this automaticly changes the
553 * attachment if there was an old one...
555 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
559 if (is_attached(aconf, cptr))
560 return ACR_ALREADY_AUTHORIZED;
561 if (IsIllegal(aconf))
562 return ACR_NO_AUTHORIZATION;
563 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
564 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
565 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
567 lp->next = cptr->confs;
568 lp->value.aconf = aconf;
571 if (aconf->status & CONF_CLIENT_MASK)
576 struct ConfItem *find_admin(void)
578 struct ConfItem *aconf;
580 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
581 if (aconf->status & CONF_ADMIN)
587 struct ConfItem* find_me(void)
589 struct ConfItem* aconf;
590 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
591 if (aconf->status & CONF_ME)
598 * attach_confs_byname
600 * Attach a CONF line to a client if the name passed matches that for
601 * the conf file (for non-C/N lines) or is an exact match (C/N lines
602 * only). The difference in behaviour is to stop C:*::* and N:*::*.
604 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
607 struct ConfItem* tmp;
608 struct ConfItem* first = NULL;
612 if (HOSTLEN < strlen(name))
615 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
616 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
617 assert(0 != tmp->name);
618 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
619 if (ACR_OK == attach_conf(cptr, tmp) && !first)
628 * Added for new access check meLazy
630 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
633 struct ConfItem* tmp;
634 struct ConfItem* first = 0;
637 if (HOSTLEN < strlen(host))
640 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
641 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
642 assert(0 != tmp->host);
643 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
644 if (ACR_OK == attach_conf(cptr, tmp) && !first)
653 * find a conf entry which matches the hostname and has the same name.
655 struct ConfItem* find_conf_exact(const char* name, const char* user,
656 const char* host, int statmask)
658 struct ConfItem *tmp;
659 char userhost[USERLEN + HOSTLEN + 3];
662 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
664 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
666 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
667 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
668 0 != ircd_strcmp(tmp->name, name))
671 * Accept if the *real* hostname (usually sockecthost)
672 * socket host) matches *either* host or name field
673 * of the configuration.
675 if (match(tmp->host, userhost))
677 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
678 if (tmp->clients < MaxLinks(tmp->confClass))
689 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
692 struct ConfItem* tmp;
695 if (HOSTLEN < strlen(name))
698 for (; lp; lp = lp->next) {
699 tmp = lp->value.aconf;
700 if (0 != (tmp->status & statmask)) {
701 assert(0 != tmp->name);
702 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
710 * Added for new access check meLazy
712 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
715 struct ConfItem* tmp = NULL;
718 if (HOSTLEN < strlen(host))
721 for (; lp; lp = lp->next) {
722 tmp = lp->value.aconf;
723 if (0 != (tmp->status & statmask)) {
724 assert(0 != tmp->host);
725 if (0 == match(tmp->host, host))
735 * Find a conf line using the IP# stored in it to search upon.
736 * Added 1/8/92 by Avalon.
738 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
741 struct ConfItem* tmp;
743 for (; lp; lp = lp->next) {
744 tmp = lp->value.aconf;
745 if (0 != (tmp->status & statmask)) {
746 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
756 * - looks for a match on all given fields.
758 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
761 struct ConfItem *bconf;
764 mask &= ~CONF_ILLEGAL;
766 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
767 if (!(bconf->status & mask) || (bconf->port != aconf->port))
770 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
771 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
773 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
776 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
777 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
779 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
781 && 0 != ircd_strcmp(bconf->passwd, "ONE")
783 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
786 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
787 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
789 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
799 * Actual REHASH service routine. Called with sig == 0 if it has been called
800 * as a result of an operator issuing this command, else assume it has been
801 * called as a result of the server receiving a HUP signal.
803 int rehash(struct Client *cptr, int sig)
805 struct ConfItem** tmp = &GlobalConfList;
806 struct ConfItem* tmp2;
807 struct ConfClass* cltmp;
808 struct Client* acptr;
809 struct MotdItem* temp;
815 sendto_opmask_butone(0, SNO_OLDSNO,
816 "Got signal SIGHUP, reloading ircd conf. file");
818 while ((tmp2 = *tmp)) {
821 * Configuration entry is still in use by some
822 * local clients, cannot delete it--mark it so
823 * that it will be deleted when the last client
826 if (!(tmp2->status & CONF_CLIENT)) {
832 tmp2->status |= CONF_ILLEGAL;
836 /* free expression trees of connect rules */
837 if ((tmp2->status & (CONF_CRULEALL | CONF_CRULEAUTO)) &&
838 (tmp2->passwd != NULL))
839 crule_free(&(tmp2->passwd));
845 * We don't delete the class table, rather mark all entries
846 * for deletion. The table is cleaned up by check_class(). - avalon
848 for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
852 * delete the juped nicks list
857 flush_resolver_cache();
859 mark_listeners_closing();
861 if (!conf_init()) /* This calls check_class(), */
862 check_class(); /* unless it fails */
865 * make sure that the server listener is re-added so it doesn't get
871 * Flush out deleted I and P lines although still in use.
873 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
874 if (!(tmp2->status & CONF_ILLEGAL))
884 for (i = 0; i <= HighestFd; i++) {
885 if ((acptr = LocalClientArray[i])) {
886 assert(!IsMe(acptr));
887 if (IsServer(acptr)) {
888 det_confs_butmask(acptr,
889 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
890 attach_confs_byname(acptr, acptr->name,
891 CONF_HUB | CONF_LEAF | CONF_UWORLD);
893 /* Because admin's are getting so uppity about people managing to
894 * get past K/G's etc, we'll "fix" the bug by actually explaining
897 if ((found_g = find_kill(acptr))) {
898 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
899 found_g == -2 ? "G-line active for %s%s" :
900 "K-line active for %s%s",
901 IsUnknown(acptr) ? "Unregistered Client ":"",
902 get_client_name(acptr, HIDE_IP));
903 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
904 "K-lined") == CPTR_KILLED)
910 * free old motd structs
922 /* reload motd files */
924 rmotd = read_motd(RPATH);
925 motd = read_motd(MPATH);
932 * Read configuration file.
934 * returns 0, if file cannot be opened
938 #define MAXCONFLINKS 150
943 static char quotes[9][2] = {
958 struct ConfItem *aconf = 0;
960 Debug((DEBUG_DEBUG, "conf_init: ircd.conf = %s", configfile));
961 if (0 == (file = fbopen(configfile, "r"))) {
964 while (fbgets(line, sizeof(line) - 1, file)) {
965 if ((tmp = strchr(line, '\n')))
968 * Do quoting of characters and # detection.
970 for (tmp = line; *tmp; tmp++) {
972 for (i = 0; quotes[i][0]; i++) {
973 if (quotes[i][0] == *(tmp + 1)) {
983 for (s = tmp; (*s = *(s + 1)); s++)
987 else if (*tmp == '#')
990 if (!*line || line[0] == '#' || line[0] == '\n' ||
991 line[0] == ' ' || line[0] == '\t')
993 /* Could we test if it's conf line at all? -Vesa */
994 if (line[1] != ':') {
995 Debug((DEBUG_ERROR, "Bad config line: %s", line));
996 sendto_op_mask(SNO_OLDSNO,"Bad Config line");
1001 aconf = make_conf();
1003 tmp = getfield(line, ':');
1007 case 'A': /* Name, e-mail address of administrator */
1008 case 'a': /* of this server. */
1009 aconf->status = CONF_ADMIN;
1011 case 'C': /* Server where I should try to connect */
1012 case 'c': /* in case of lp failures */
1014 aconf->status = CONF_SERVER;
1018 aconf->status = CONF_CRULEALL;
1020 /* Connect rule - autos only */
1022 aconf->status = CONF_CRULEAUTO;
1024 case 'H': /* Hub server line */
1026 aconf->status = CONF_HUB;
1028 case 'I': /* Just plain normal irc client trying */
1029 case 'i': /* to connect me */
1030 aconf->status = CONF_CLIENT;
1032 case 'K': /* Kill user line on irc.conf */
1033 aconf->status = CONF_KILL;
1035 case 'k': /* Kill user line based on IP in ircd.conf */
1036 aconf->status = CONF_IPKILL;
1038 /* Operator. Line should contain at least */
1039 /* password and host where connection is */
1040 case 'L': /* guaranteed leaf server */
1042 aconf->status = CONF_LEAF;
1044 /* Me. Host field is name used for this host */
1045 /* and port number is the number of the port */
1048 aconf->status = CONF_ME;
1051 aconf->status = CONF_OPERATOR;
1053 /* Local Operator, (limited privs --SRB) */
1055 aconf->status = CONF_LOCOP;
1057 case 'P': /* listen port line */
1059 aconf->status = CONF_LISTEN_PORT;
1061 case 'T': /* print out different motd's */
1062 case 't': /* based on hostmask */
1063 aconf->status = CONF_TLINES;
1065 case 'U': /* Underworld server, allowed to hack modes */
1066 case 'u': /* *Every* server on the net must define the same !!! */
1067 aconf->status = CONF_UWORLD;
1071 aconf->status = CONF_CLASS;
1074 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1075 sendto_op_mask(SNO_OLDSNO,"Unknown prefix in config file: %c",*tmp);
1078 if (IsIllegal(aconf))
1081 for (;;) { /* Fake loop, that I can use break here --msa */
1082 if ((tmp = getfield(NULL, ':')) == NULL)
1084 DupString(aconf->host, tmp);
1085 if ((tmp = getfield(NULL, (aconf->status == CONF_KILL
1086 || aconf->status == CONF_IPKILL) ? '"' : ':')) == NULL)
1088 DupString(aconf->passwd, tmp);
1089 if ((tmp = getfield(NULL, ':')) == NULL)
1091 DupString(aconf->name, tmp);
1092 if ((tmp = getfield(NULL, ':')) == NULL)
1094 aconf->port = atoi(tmp);
1095 tmp = getfield(NULL, ':');
1096 if (aconf->status & CONF_ME) {
1098 Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1099 "assigned to you by routing-com!\n"));
1100 ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1101 "assigned to you by routing-com!\n");
1104 SetYXXServerName(&me, atoi(tmp)); /* Our Numeric Nick */
1107 aconf->confClass = find_class(atoi(tmp));
1111 * If conf line is a class definition, create a class entry
1112 * for it and make the conf_line illegal and delete it.
1114 if (aconf->status & CONF_CLASS) {
1115 add_class(atoi(aconf->host), atoi(aconf->passwd),
1116 atoi(aconf->name), aconf->port, tmp ? atoi(tmp) : 0);
1120 * Associate each conf line with a class by using a pointer
1121 * to the correct class record. -avalon
1123 if (aconf->status & CONF_CLIENT_MASK) {
1124 if (aconf->confClass == 0)
1125 aconf->confClass = find_class(0);
1127 if (aconf->status & CONF_LISTEN_PORT) {
1130 if (!EmptyString(aconf->name)) {
1131 const char* x = aconf->name;
1132 if ('S' == ToUpper(*x))
1135 if ('H' == ToUpper(*x))
1138 add_listener(aconf->port, aconf->passwd, aconf->host,
1139 is_server, is_hidden);
1142 if (aconf->status & CONF_CLIENT) {
1143 struct ConfItem *bconf;
1145 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1147 bconf->status &= ~CONF_ILLEGAL;
1148 if (aconf->status == CONF_CLIENT) {
1150 * copy the password field in case it changed
1152 MyFree(bconf->passwd);
1153 bconf->passwd = aconf->passwd;
1156 ConfLinks(bconf) -= bconf->clients;
1157 bconf->confClass = aconf->confClass;
1158 if (bconf->confClass)
1159 ConfLinks(bconf) += bconf->clients;
1165 if (aconf->status & CONF_SERVER) {
1166 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1167 strchr(aconf->host, '?') || !aconf->name)
1170 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1171 if (!strchr(aconf->host, '@')) {
1173 int len = 3; /* *@\0 = 3 */
1175 len += strlen(aconf->host);
1176 newhost = (char*) MyMalloc(len);
1177 assert(0 != newhost);
1178 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1179 MyFree(aconf->host);
1180 aconf->host = newhost;
1183 if (aconf->status & CONF_SERVER) {
1184 if (EmptyString(aconf->passwd))
1186 lookup_confhost(aconf);
1189 /* Create expression tree from connect rule...
1190 * If there's a parsing error, nuke the conf structure */
1191 if (aconf->status & (CONF_CRULEALL | CONF_CRULEAUTO)) {
1192 MyFree(aconf->passwd);
1193 if ((aconf->passwd = (char *)crule_parse(aconf->name)) == NULL) {
1201 * Own port and name cannot be changed after the startup.
1202 * (or could be allowed, but only if all links are closed first).
1203 * Configuration info does not override the name and port
1204 * if previously defined. Note, that "info"-field can be
1205 * changed by "/rehash".
1207 if (aconf->status == CONF_ME) {
1208 ircd_strncpy(me.info, aconf->name, REALLEN);
1211 if (aconf->status == CONF_IPKILL) {
1213 * Here we use the same kludge as in listener.c to parse
1214 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
1220 class=sscanf(aconf->host,"%d.%d.%d.%d/%d",
1221 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
1223 aconf->bits=class*8;
1228 sprintf_irc(ipname,"%d.%d.%d.%d",ad[0], ad[1], ad[2], ad[3]);
1230 /* This ensures endian correctness */
1231 aconf->ipnum.s_addr = inet_addr(ipname);
1232 Debug((DEBUG_DEBUG,"IPkill: %s = %08x/%i (%08x)",ipname,
1233 aconf->ipnum.s_addr,aconf->bits,NETMASK(aconf->bits)));
1237 * Juped nicks are listed in the 'password' field of U:lines,
1238 * the list is comma separated and might be empty and/or contain
1239 * empty elements... the only limit is that it MUST be shorter
1240 * than 512 chars, or they will be cutted out :)
1242 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1243 addNickJupes(aconf->passwd);
1245 if (aconf->status & CONF_ADMIN) {
1246 if (!aconf->host || !aconf->passwd || !aconf->name) {
1247 Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
1248 ircd_log(L_WARNING, "Your A: line must have 4 fields!\n");
1252 collapse(aconf->host);
1253 collapse(aconf->name);
1254 Debug((DEBUG_NOTICE,
1255 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1256 aconf->status, aconf->host, aconf->passwd,
1257 aconf->name, aconf->port, aconf->confClass));
1258 aconf->next = GlobalConfList;
1259 GlobalConfList = aconf;
1266 nextping = nextconnect = CurrentTime;
1271 * Read info from T:lines into TRecords which include the file
1272 * timestamp, the hostmask, and the contents of the motd file
1277 struct ConfItem *tmp;
1278 struct TRecord *temp;
1279 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1280 struct MotdItem *amotd;
1282 /* Free the old trecords and the associated motd contents first */
1286 while (tdata->tmotd)
1288 amotd = tdata->tmotd->next;
1289 MyFree(tdata->tmotd);
1290 tdata->tmotd = amotd;
1296 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1297 if (tmp->status == CONF_TLINES && tmp->host && tmp->passwd) {
1298 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1301 temp->hostmask = tmp->host;
1302 temp->tmotd = read_motd(tmp->passwd);
1303 temp->tmotd_tm = motd_tm;
1319 * 0: Client may continue to try and connect
1320 * -1: Client was K/k:'d - sideeffect: reason was sent.
1321 * -2: Client was G/g:'d - sideeffect: reason was sent.
1323 * Client may have been sent a reason why they are denied, as above.
1325 int find_kill(struct Client *cptr)
1329 struct ConfItem* tmp;
1330 struct Gline* agline = NULL;
1337 host = cptr->sockhost;
1338 name = cptr->user->username;
1340 assert(strlen(host) <= HOSTLEN);
1341 assert((name ? strlen(name) : 0) <= HOSTLEN);
1343 /* 2000-07-14: Rewrote this loop for massive speed increases.
1346 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1348 if ((tmp->status & CONF_KLINE) == 0)
1352 * What is this for? You can K: by port?!
1355 if (tmp->port && tmp->port != cptr->listener->port)
1358 if (!tmp->name || match(tmp->name, name) != 0)
1364 if (tmp->status != CONF_IPKILL) {
1365 if (match(tmp->host, host) == 0)
1368 else { /* k: by IP */
1369 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1370 cptr->ip.s_addr, tmp->ipnum.s_addr, tmp->bits,
1371 NETMASK(tmp->bits)));
1372 if ((cptr->ip.s_addr & NETMASK(tmp->bits)) == tmp->ipnum.s_addr)
1377 if (EmptyString(tmp->passwd))
1378 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1379 ":Connection from your host is refused on this server.");
1381 if (*tmp->passwd == '"') {
1382 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1383 strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1385 else if (*tmp->passwd == '!')
1386 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1388 #ifdef COMMENT_IS_FILE
1389 killcomment(cptr, cptr->name, tmp->passwd);
1391 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1397 /* find active glines */
1398 /* added a check against the user's IP address to find_gline() -Kev */
1399 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1400 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1401 GlineReason(agline));
1403 agline = NULL; /* if a gline was found, it was inactive */
1413 struct MotdItem* read_motd(const char* motdfile)
1415 FBFILE* file = NULL;
1416 struct MotdItem* temp;
1417 struct MotdItem* newmotd;
1418 struct MotdItem* last;
1423 if (NULL == (file = fbopen(motdfile, "r"))) {
1424 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1427 if (-1 == fbstat(&sb, file)) {
1431 newmotd = last = NULL;
1432 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1433 while (fbgets(line, sizeof(line) - 1, file)) {
1434 if ((tmp = (char *)strchr(line, '\n')))
1436 if ((tmp = (char *)strchr(line, '\r')))
1438 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1440 strcpy(temp->line, line);
1454 * Ordinary client access check. Look for conf lines which have the same
1455 * status as the flags passed.
1457 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1459 enum AuthorizationCheckResult acr = ACR_OK;
1463 if ((acr = attach_iline(cptr))) {
1464 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1465 cptr->name, cptr->sockhost));
1474 * Check access for a server given its name (passed in cptr struct).
1475 * Must check for all C/N lines which have a name which matches the
1476 * name given and a host which matches. A host alias which is the
1477 * same as the server name is also acceptable in the host field of a
1482 * -1 = Access denied
1485 int conf_check_server(struct Client *cptr)
1487 struct ConfItem* c_conf = NULL;
1490 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1491 cptr->name, cptr->sockhost));
1493 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1494 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1499 * We initiated this connection so the client should have a C and N
1500 * line already attached after passing through the connect_server()
1503 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1504 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1506 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1508 det_confs_butmask(cptr, 0);
1516 if (cptr->dns_reply) {
1518 struct hostent* hp = cptr->dns_reply->hp;
1519 const char* name = hp->h_name;
1521 * If we are missing a C or N line from above, search for
1522 * it under all known hostnames we have for this ip#.
1524 for (i = 0; name; name = hp->h_aliases[i++]) {
1525 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1526 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1531 for (i = 0; hp->h_addr_list[i]; i++) {
1532 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1539 * Check for C lines with the hostname portion the ip number
1540 * of the host the server runs on. This also checks the case where
1541 * there is a server connecting from 'localhost'.
1543 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1547 * Attach by IP# only if all other checks have failed.
1548 * It is quite possible to get here with the strange things that can
1549 * happen when using DNS in the way the irc server does. -avalon
1552 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1554 * detach all conf lines that got attached by attach_confs()
1556 det_confs_butmask(cptr, 0);
1558 * if no C or no N lines, then deny access
1561 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1562 cptr->name, cptr->username, cptr->sockhost));
1565 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1567 * attach the C and N lines to the client structure for later use.
1569 attach_conf(cptr, c_conf);
1570 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1572 if (INADDR_NONE == c_conf->ipnum.s_addr)
1573 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1575 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));