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_string.h"
48 #include "sprintf_irc.h"
54 #include <arpa/inet.h>
65 #define INADDR_NONE 0xffffffff
68 struct ConfItem* GlobalConfList = 0;
69 int GlobalConfCount = 0;
70 struct MotdItem* motd = NULL;
71 struct MotdItem* rmotd = NULL;
72 struct TRecord* tdata = NULL;
77 * is the K line field an interval or a comment? - Mmmm
79 static int is_comment(const char *comment)
82 unsigned int len = strlen(comment);
83 for (i = 0; i < len; ++i) {
84 if (!IsKTimeChar(comment[i]))
91 * check against a set of time intervals
93 static int check_time_interval(char *interval, char *reply)
100 int perm_max_minutes;
105 tptr = localtime(&CurrentTime);
106 nowm = tptr->tm_hour * 60 + tptr->tm_min;
109 p = strchr(interval, ',');
112 if (sscanf(interval, "%2d%2d-%2d%2d", &perm_min_hours, &perm_min_minutes,
113 &perm_max_hours, &perm_max_minutes) != 4)
121 perm_min = 60 * perm_min_hours + perm_min_minutes;
122 perm_max = 60 * perm_max_hours + perm_max_minutes;
124 * The following check allows intervals over midnight ...
126 if ((perm_min < perm_max)
127 ? (perm_min <= nowm && nowm <= perm_max)
128 : (perm_min <= nowm || nowm <= perm_max))
130 sprintf_irc(reply, ":%%s %%d %%s :%s %d:%02d to %d:%02d.",
131 "You are not allowed to connect from",
132 perm_min_hours, perm_min_minutes,
133 perm_max_hours, perm_max_minutes);
134 return (ERR_YOUREBANNEDCREEP);
136 if ((perm_min < perm_max)
137 ? (perm_min <= nowm + 5 && nowm + 5 <= perm_max)
138 : (perm_min <= nowm + 5 || nowm + 5 <= perm_max))
140 sprintf_irc(reply, ":%%s %%d %%s :%d minute%s%s",
141 perm_min - nowm, (perm_min - nowm) > 1 ? "s " : " ",
142 "and you will be denied for further access");
143 return (ERR_YOUWILLBEBANNED);
151 * output the reason for being k lined from a file - Mmmm
153 * parv is the sender prefix
154 * filename is the file that is to be output to the K lined client
156 static void killcomment(struct Client *sptr, char *parv, char *filename)
164 if (NULL == (file = fbopen(filename, "r"))) {
165 sendto_one(sptr, err_str(ERR_NOMOTD), me.name, parv);
167 ":%s %d %s :Connection from your host is refused on this server.",
168 me.name, ERR_YOUREBANNEDCREEP, parv);
172 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
173 while (fbgets(line, sizeof(line) - 1, file)) {
174 if ((tmp = strchr(line, '\n')))
176 if ((tmp = strchr(line, '\r')))
180 * me.name, ERR_YOUREBANNEDCREEP, parv,line); */
181 sendto_one(sptr, rpl_str(RPL_MOTD), me.name, parv, line);
184 ":%s %d %s :Connection from your host is refused on this server.",
185 me.name, ERR_YOUREBANNEDCREEP, parv);
189 struct ConfItem* make_conf(void)
191 struct ConfItem* aconf;
193 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
198 aconf->status = CONF_ILLEGAL;
200 aconf->ipnum.s_addr = INADDR_NONE;
206 aconf->dns_pending = 0;
207 aconf->confClass = 0;
212 void delist_conf(struct ConfItem *aconf)
214 if (aconf == GlobalConfList)
215 GlobalConfList = GlobalConfList->next;
217 struct ConfItem *bconf;
219 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
221 bconf->next = aconf->next;
226 void free_conf(struct ConfItem *aconf)
228 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
229 aconf->host ? aconf->host : "*",
230 aconf->name ? aconf->name : "*",
232 if (aconf->dns_pending)
233 delete_resolver_queries(aconf);
236 memset(aconf->passwd, 0, strlen(aconf->passwd));
237 MyFree(aconf->passwd);
246 * conf_dns_callback - called when resolver query finishes
247 * if the query resulted in a successful search, hp will contain
248 * a non-null pointer, otherwise hp will be null.
249 * if successful save hp in the conf item it was called with
251 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
253 struct ConfItem* aconf = (struct ConfItem*) vptr;
254 aconf->dns_pending = 0;
256 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
260 * conf_dns_lookup - do a nameserver lookup of the conf host
261 * if the conf entry is currently doing a ns lookup do nothing, otherwise
262 * if the lookup returns a null pointer, set the conf dns_pending flag
264 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
266 struct DNSReply* dns_reply = 0;
267 if (!aconf->dns_pending) {
268 char buf[HOSTLEN + 1];
269 struct DNSQuery query;
271 query.callback = conf_dns_callback;
272 host_from_uh(buf, aconf->host, HOSTLEN);
275 if (0 == (dns_reply = gethost_byname(buf, &query)))
276 aconf->dns_pending = 1;
285 * Do (start) DNS lookups of all hostnames in the conf line and convert
286 * an IP addresses in a.b.c.d number for to IP#s.
288 static void lookup_confhost(struct ConfItem *aconf)
290 struct DNSReply* reply;
292 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
293 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
294 aconf->host, aconf->name));
298 * Do name lookup now on hostnames given and store the
299 * ip numbers in conf structure.
301 if (IsDigit(*aconf->host)) {
303 * rfc 1035 sez host names may not start with a digit
304 * XXX - this has changed code needs to be updated
306 aconf->ipnum.s_addr = inet_addr(aconf->host);
307 if (INADDR_NONE == aconf->ipnum.s_addr) {
308 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
309 aconf->host, aconf->name));
312 else if ((reply = conf_dns_lookup(aconf)))
313 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
317 * conf_find_server - find a server by name or hostname
318 * returns a server conf item pointer if found, 0 otherwise
320 * NOTE: at some point when we don't have to scan the entire
321 * list it may be cheaper to look for server names and host
322 * names in separate loops (original code did it that way)
324 struct ConfItem* conf_find_server(const char* name)
326 struct ConfItem* conf;
329 for (conf = GlobalConfList; conf; conf = conf->next) {
330 if (CONF_SERVER == conf->status) {
332 * Check first servernames, then try hostnames.
333 * XXX - match returns 0 if there _is_ a match... guess they
334 * haven't decided what true is yet
336 if (0 == match(name, conf->name))
344 * conf_eval_crule - evaluate connection rules
345 * returns the name of the rule triggered if found, 0 otherwise
347 * Evaluate connection rules... If no rules found, allow the
348 * connect. Otherwise stop with the first true rule (ie: rules
349 * are ored together. Oper connects are effected only by D
350 * lines (CRULEALL) not d lines (CRULEAUTO).
352 const char* conf_eval_crule(struct ConfItem* conf)
354 struct ConfItem* rule;
357 for (rule = GlobalConfList; rule; rule = rule->next) {
358 if ((CONF_CRULEALL == rule->status) && (0 == match(rule->host, conf->name))) {
359 if (crule_eval(rule->passwd))
368 * field breakup for ircd.conf file.
370 static char* getfield(char* newline, char fs)
372 static char* gfline = NULL;
382 end = field = gfline;
398 } while (end && fs != ':' && *++end != ':' && *end != '\n') ;
402 if ((end = strchr(field, '\n')) == NULL)
403 end = field + strlen(field);
414 * Remove all conf entries from the client except those which match
415 * the status field mask.
417 void det_confs_butmask(struct Client *cptr, int mask)
419 struct SLink *tmp, *tmp2;
421 for (tmp = cptr->confs; tmp; tmp = tmp2) {
423 if ((tmp->value.aconf->status & mask) == 0)
424 detach_conf(cptr, tmp->value.aconf);
429 * validate_hostent - make sure hostnames are valid in a hostent struct
430 * XXX - this is terrible, what's worse is it used to be in the inner
431 * loop of scanning all the I:lines --Bleep
433 static int validate_hostent(struct hostent* hp)
435 char fullname[HOSTLEN + 1];
440 for (hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
441 unsigned int fullnamelen = 0;
442 unsigned int label_count = 0;
444 ircd_strncpy(fullname, hname, HOSTLEN);
445 fullname[HOSTLEN] = '\0';
447 * Disallow a hostname label to contain anything but a [-a-zA-Z0-9].
448 * It may not start or end on a '.'.
449 * A label may not end on a '-', the maximum length of a label is
451 * On top of that (which seems to be the RFC) we demand that the
452 * top domain does not contain any digits.
454 error = (*hname == '.') ? 1 : 0; /* May not start with a '.' */
457 for (p = fullname; *p; ++p, ++fullnamelen) {
459 /* Label may not end on '-' and May not end on a '.' */
460 if (p[-1] == '-' || p[1] == 0) {
465 error = 0; /* Was not top domain */
468 if (++label_count > 63) {
469 /* Label not longer then 63 */
473 if (*p >= '0' && *p <= '9') {
474 /* In case this is top domain */
478 if (!(*p >= 'a' && *p <= 'z')
479 && !(*p >= 'A' && *p <= 'Z') && *p != '-') {
492 * check_limit_and_attach - check client limits and attach I:line
494 static int check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
497 /* Special case: exactly one digit */
498 if (IsDigit(*aconf->passwd) && !aconf->passwd[1]) {
500 * Refuse connections when there are already <digit>
501 * clients connected with the same IP number
503 unsigned short nr = *aconf->passwd - '0';
504 if (IPcheck_nr(cptr) > nr)
505 return ACR_TOO_MANY_FROM_IP; /* Already got nr with that ip# */
508 else if (0 == strcmp(aconf->passwd, "ONE")) {
510 for (i = HighestFd; i > -1; --i) {
511 if (LocalClientArray[i] && MyUser(LocalClientArray[i]) &&
512 LocalClientArray[i]->ip.s_addr == cptr->ip.s_addr)
513 return ACR_TOO_MANY_FROM_IP; /* Already got one with that ip# */
518 return attach_conf(cptr, aconf);
522 * Find the first (best) I line to attach.
524 int attach_iline(struct Client* cptr)
526 struct ConfItem* aconf;
529 static char uhost[HOSTLEN + USERLEN + 3];
530 static char fullname[HOSTLEN + 1];
531 struct hostent* hp = 0;
533 if (cptr->dns_reply) {
534 hp = cptr->dns_reply->hp;
535 if (!validate_hostent(hp))
538 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
539 if (aconf->status != CONF_CLIENT)
541 if (aconf->port && aconf->port != cptr->listener->port)
543 if (!aconf->host || !aconf->name)
546 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
547 ircd_strncpy(fullname, hname, HOSTLEN);
548 fullname[HOSTLEN] = '\0';
550 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
552 if (strchr(aconf->name, '@')) {
553 strcpy(uhost, cptr->username);
558 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
559 uhost[sizeof(uhost) - 1] = 0;
560 if (0 == match(aconf->name, uhost)) {
561 if (strchr(uhost, '@'))
562 cptr->flags |= FLAGS_DOID;
563 return check_limit_and_attach(cptr, aconf);
567 if (strchr(aconf->host, '@')) {
568 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
569 uhost[sizeof(uhost) - 2] = 0;
574 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
575 uhost[sizeof(uhost) - 1] = 0;
576 if (match(aconf->host, uhost))
578 if (strchr(uhost, '@'))
579 cptr->flags |= FLAGS_DOID;
581 return check_limit_and_attach(cptr, aconf);
583 return ACR_NO_AUTHORIZATION;
587 * detach_conf - Disassociate configuration from the client.
589 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
596 assert(0 < aconf->clients);
601 if ((*lp)->value.aconf == aconf) {
602 if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) &&
603 ConfLinks(aconf) > 0)
605 if (0 == --aconf->clients && IsIllegal(aconf))
618 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
622 for (lp = cptr->confs; lp; lp = lp->next)
623 if (lp->value.aconf == aconf)
632 * Associate a specific configuration entry to a *local*
633 * client (this is the one which used in accepting the
634 * connection). Note, that this automaticly changes the
635 * attachment if there was an old one...
637 int attach_conf(struct Client *cptr, struct ConfItem *aconf)
641 if (is_attached(aconf, cptr))
642 return ACR_ALREADY_AUTHORIZED;
643 if (IsIllegal(aconf))
644 return ACR_NO_AUTHORIZATION;
645 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
646 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
647 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
649 lp->next = cptr->confs;
650 lp->value.aconf = aconf;
653 if (aconf->status & CONF_CLIENT_MASK)
658 struct ConfItem *find_admin(void)
660 struct ConfItem *aconf;
662 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
663 if (aconf->status & CONF_ADMIN)
669 struct ConfItem* find_me(void)
671 struct ConfItem* aconf;
672 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
673 if (aconf->status & CONF_ME)
680 * attach_confs_byname
682 * Attach a CONF line to a client if the name passed matches that for
683 * the conf file (for non-C/N lines) or is an exact match (C/N lines
684 * only). The difference in behaviour is to stop C:*::* and N:*::*.
686 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
689 struct ConfItem* tmp;
690 struct ConfItem* first = NULL;
694 if (HOSTLEN < strlen(name))
697 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
698 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
699 assert(0 != tmp->name);
700 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
701 if (ACR_OK == attach_conf(cptr, tmp) && !first)
710 * Added for new access check meLazy
712 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
715 struct ConfItem* tmp;
716 struct ConfItem* first = 0;
719 if (HOSTLEN < strlen(host))
722 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
723 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
724 assert(0 != tmp->host);
725 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
726 if (ACR_OK == attach_conf(cptr, tmp) && !first)
735 * find a conf entry which matches the hostname and has the same name.
737 struct ConfItem* find_conf_exact(const char* name, const char* user,
738 const char* host, int statmask)
740 struct ConfItem *tmp;
741 char userhost[USERLEN + HOSTLEN + 3];
744 * XXX - buffer overflow possible, unchecked variables
747 sprintf_irc(userhost, "%s@%s", user, host);
749 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
751 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
752 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
753 0 != ircd_strcmp(tmp->name, name))
756 * Accept if the *real* hostname (usually sockecthost)
757 * socket host) matches *either* host or name field
758 * of the configuration.
760 if (match(tmp->host, userhost))
762 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
763 if (tmp->clients < MaxLinks(tmp->confClass))
774 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
777 struct ConfItem* tmp;
780 if (HOSTLEN < strlen(name))
783 for (; lp; lp = lp->next) {
784 tmp = lp->value.aconf;
785 if (0 != (tmp->status & statmask)) {
786 assert(0 != tmp->name);
787 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
795 * Added for new access check meLazy
797 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
800 struct ConfItem* tmp;
803 if (HOSTLEN < strlen(host))
806 for (; lp; lp = lp->next) {
807 tmp = lp->value.aconf;
808 if (0 != (tmp->status & statmask)) {
809 assert(0 != tmp->host);
810 if (0 == match(tmp->host, host))
820 * Find a conf line using the IP# stored in it to search upon.
821 * Added 1/8/92 by Avalon.
823 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
826 struct ConfItem* tmp;
828 for (; lp; lp = lp->next) {
829 tmp = lp->value.aconf;
830 if (0 != (tmp->status & statmask)) {
831 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
841 * - looks for a match on all given fields.
843 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
846 struct ConfItem *bconf;
849 mask &= ~CONF_ILLEGAL;
851 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
852 if (!(bconf->status & mask) || (bconf->port != aconf->port))
855 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
856 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
858 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
861 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
862 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
864 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
866 && 0 != ircd_strcmp(bconf->passwd, "ONE")
868 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
871 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
872 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
874 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
884 * Actual REHASH service routine. Called with sig == 0 if it has been called
885 * as a result of an operator issuing this command, else assume it has been
886 * called as a result of the server receiving a HUP signal.
888 int rehash(struct Client *cptr, int sig)
890 struct ConfItem** tmp = &GlobalConfList;
891 struct ConfItem* tmp2;
892 struct ConfClass* cltmp;
893 struct Client* acptr;
894 struct MotdItem* temp;
900 sendto_ops("Got signal SIGHUP, reloading ircd conf. file");
902 while ((tmp2 = *tmp)) {
905 * Configuration entry is still in use by some
906 * local clients, cannot delete it--mark it so
907 * that it will be deleted when the last client
910 if (!(tmp2->status & CONF_CLIENT)) {
916 tmp2->status |= CONF_ILLEGAL;
920 /* free expression trees of connect rules */
921 if ((tmp2->status & (CONF_CRULEALL | CONF_CRULEAUTO)) &&
922 (tmp2->passwd != NULL))
923 crule_free(&(tmp2->passwd));
929 * We don't delete the class table, rather mark all entries
930 * for deletion. The table is cleaned up by check_class(). - avalon
932 for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
936 * delete the juped nicks list
941 flush_resolver_cache();
943 mark_listeners_closing();
945 if (initconf(0) == -1) /* This calls check_class(), */
946 check_class(); /* unless it fails */
949 * make sure that the server listener is re-added so it doesn't get
955 * Flush out deleted I and P lines although still in use.
957 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
958 if (!(tmp2->status & CONF_ILLEGAL))
968 for (i = 0; i <= HighestFd; i++) {
969 if ((acptr = LocalClientArray[i])) {
970 assert(!IsMe(acptr));
971 if (IsServer(acptr)) {
972 det_confs_butmask(acptr,
973 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
974 attach_confs_byname(acptr, acptr->name,
975 CONF_HUB | CONF_LEAF | CONF_UWORLD);
977 if ((found_g = find_kill(acptr))) {
978 sendto_op_mask(found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
979 found_g == -2 ? "G-line active for %s" : "K-line active for %s",
980 get_client_name(acptr, HIDE_IP));
981 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
982 "K-lined") == CPTR_KILLED)
988 * free old motd structs
1000 /* reload motd files */
1002 rmotd = read_motd(RPATH);
1003 motd = read_motd(MPATH);
1010 * Read configuration file.
1012 * returns -1, if file cannot be opened
1016 #define MAXCONFLINKS 150
1019 int initconf(int opt)
1021 static char quotes[9][2] = {
1036 struct ConfItem *aconf = 0;
1038 Debug((DEBUG_DEBUG, "initconf(): ircd.conf = %s", configfile));
1039 if (0 == (file = fbopen(configfile, "r"))) {
1042 while (fbgets(line, sizeof(line) - 1, file)) {
1043 if ((tmp = strchr(line, '\n')))
1046 * Do quoting of characters and # detection.
1048 for (tmp = line; *tmp; tmp++) {
1050 for (i = 0; quotes[i][0]; i++) {
1051 if (quotes[i][0] == *(tmp + 1)) {
1052 *tmp = quotes[i][1];
1061 for (s = tmp; (*s = *(s + 1)); s++)
1065 else if (*tmp == '#')
1068 if (!*line || line[0] == '#' || line[0] == '\n' ||
1069 line[0] == ' ' || line[0] == '\t')
1071 /* Could we test if it's conf line at all? -Vesa */
1072 if (line[1] != ':') {
1073 Debug((DEBUG_ERROR, "Bad config line: %s", line));
1078 aconf = make_conf();
1080 tmp = getfield(line, ':');
1084 case 'A': /* Name, e-mail address of administrator */
1085 case 'a': /* of this server. */
1086 aconf->status = CONF_ADMIN;
1088 case 'C': /* Server where I should try to connect */
1089 case 'c': /* in case of lp failures */
1091 aconf->status = CONF_SERVER;
1095 aconf->status = CONF_CRULEALL;
1097 /* Connect rule - autos only */
1099 aconf->status = CONF_CRULEAUTO;
1101 case 'H': /* Hub server line */
1103 aconf->status = CONF_HUB;
1105 case 'I': /* Just plain normal irc client trying */
1106 case 'i': /* to connect me */
1107 aconf->status = CONF_CLIENT;
1109 case 'K': /* Kill user line on irc.conf */
1110 aconf->status = CONF_KILL;
1112 case 'k': /* Kill user line based on IP in ircd.conf */
1113 aconf->status = CONF_IPKILL;
1115 /* Operator. Line should contain at least */
1116 /* password and host where connection is */
1117 case 'L': /* guaranteed leaf server */
1119 aconf->status = CONF_LEAF;
1121 /* Me. Host field is name used for this host */
1122 /* and port number is the number of the port */
1125 aconf->status = CONF_ME;
1128 aconf->status = CONF_OPERATOR;
1130 /* Local Operator, (limited privs --SRB) */
1132 aconf->status = CONF_LOCOP;
1134 case 'P': /* listen port line */
1136 aconf->status = CONF_LISTEN_PORT;
1138 case 'T': /* print out different motd's */
1139 case 't': /* based on hostmask */
1140 aconf->status = CONF_TLINES;
1142 case 'U': /* Underworld server, allowed to hack modes */
1143 case 'u': /* *Every* server on the net must define the same !!! */
1144 aconf->status = CONF_UWORLD;
1148 aconf->status = CONF_CLASS;
1151 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1154 if (IsIllegal(aconf))
1157 for (;;) { /* Fake loop, that I can use break here --msa */
1158 if ((tmp = getfield(NULL, ':')) == NULL)
1160 DupString(aconf->host, tmp);
1161 if ((tmp = getfield(NULL, (aconf->status == CONF_KILL
1162 || aconf->status == CONF_IPKILL) ? '"' : ':')) == NULL)
1164 DupString(aconf->passwd, tmp);
1165 if ((tmp = getfield(NULL, ':')) == NULL)
1167 DupString(aconf->name, tmp);
1168 if ((tmp = getfield(NULL, ':')) == NULL)
1170 aconf->port = atoi(tmp);
1171 tmp = getfield(NULL, ':');
1172 if (aconf->status & CONF_ME) {
1174 Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1175 "assigned to you by routing-com, behind the port number!\n"));
1176 ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1177 "assigned to you by routing-com, behind the port number!\n");
1180 SetYXXServerName(&me, atoi(tmp)); /* Our Numeric Nick */
1183 aconf->confClass = find_class(atoi(tmp));
1187 * If conf line is a class definition, create a class entry
1188 * for it and make the conf_line illegal and delete it.
1190 if (aconf->status & CONF_CLASS) {
1191 add_class(atoi(aconf->host), atoi(aconf->passwd),
1192 atoi(aconf->name), aconf->port, tmp ? atoi(tmp) : 0);
1196 * Associate each conf line with a class by using a pointer
1197 * to the correct class record. -avalon
1199 if (aconf->status & CONF_CLIENT_MASK) {
1200 if (aconf->confClass == 0)
1201 aconf->confClass = find_class(0);
1203 if (aconf->status & CONF_LISTEN_PORT) {
1206 if (!EmptyString(aconf->name)) {
1207 const char* x = aconf->name;
1208 if ('S' == ToUpper(*x))
1211 if ('H' == ToUpper(*x))
1214 add_listener(aconf->port, aconf->passwd, aconf->host,
1215 is_server, is_hidden);
1218 if (aconf->status & CONF_CLIENT) {
1219 struct ConfItem *bconf;
1221 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1223 bconf->status &= ~CONF_ILLEGAL;
1224 if (aconf->status == CONF_CLIENT) {
1226 * copy the password field in case it changed
1228 MyFree(bconf->passwd);
1229 bconf->passwd = aconf->passwd;
1232 ConfLinks(bconf) -= bconf->clients;
1233 bconf->confClass = aconf->confClass;
1234 if (bconf->confClass)
1235 ConfLinks(bconf) += bconf->clients;
1241 if (aconf->status & CONF_SERVER) {
1242 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1243 strchr(aconf->host, '?') || !aconf->name)
1246 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1247 if (!strchr(aconf->host, '@')) {
1249 int len = 3; /* *@\0 = 3 */
1251 len += strlen(aconf->host);
1252 newhost = (char*) MyMalloc(len);
1253 assert(0 != newhost);
1254 sprintf_irc(newhost, "*@%s", aconf->host);
1255 MyFree(aconf->host);
1256 aconf->host = newhost;
1259 if (aconf->status & CONF_SERVER) {
1260 if (EmptyString(aconf->passwd))
1262 else if (!(opt & BOOT_QUICK))
1263 lookup_confhost(aconf);
1266 /* Create expression tree from connect rule...
1267 * If there's a parsing error, nuke the conf structure */
1268 if (aconf->status & (CONF_CRULEALL | CONF_CRULEAUTO)) {
1269 MyFree(aconf->passwd);
1270 if ((aconf->passwd = (char *)crule_parse(aconf->name)) == NULL) {
1278 * Own port and name cannot be changed after the startup.
1279 * (or could be allowed, but only if all links are closed first).
1280 * Configuration info does not override the name and port
1281 * if previously defined. Note, that "info"-field can be
1282 * changed by "/rehash".
1284 if (aconf->status == CONF_ME) {
1285 ircd_strncpy(me.info, aconf->name, REALLEN);
1289 * Juped nicks are listed in the 'password' field of U:lines,
1290 * the list is comma separated and might be empty and/or contain
1291 * empty elements... the only limit is that it MUST be shorter
1292 * than 512 chars, or they will be cutted out :)
1294 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1295 addNickJupes(aconf->passwd);
1297 if (aconf->status & CONF_ADMIN) {
1298 if (!aconf->host || !aconf->passwd || !aconf->name) {
1299 Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
1300 ircd_log(L_WARNING, "Your A: line must have 4 fields!\n");
1304 collapse(aconf->host);
1305 collapse(aconf->name);
1306 Debug((DEBUG_NOTICE,
1307 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1308 aconf->status, aconf->host, aconf->passwd,
1309 aconf->name, aconf->port, aconf->confClass));
1310 aconf->next = GlobalConfList;
1311 GlobalConfList = aconf;
1318 nextping = nextconnect = CurrentTime;
1323 * Read info from T:lines into TRecords which include the file
1324 * timestamp, the hostmask, and the contents of the motd file
1329 struct ConfItem *tmp;
1330 struct TRecord *temp;
1331 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1332 struct MotdItem *amotd;
1334 /* Free the old trecords and the associated motd contents first */
1338 while (tdata->tmotd)
1340 amotd = tdata->tmotd->next;
1341 MyFree(tdata->tmotd);
1342 tdata->tmotd = amotd;
1348 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1349 if (tmp->status == CONF_TLINES && tmp->host && tmp->passwd) {
1350 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1353 temp->hostmask = tmp->host;
1354 temp->tmotd = read_motd(tmp->passwd);
1355 temp->tmotd_tm = motd_tm;
1366 int find_kill(struct Client *cptr)
1371 struct ConfItem* tmp;
1372 struct Gline* agline = NULL;
1379 host = cptr->sockhost;
1380 name = cptr->user->username;
1385 * XXX - if this ever happens, we're already screwed
1387 if (strlen(host) > HOSTLEN ||
1388 (name ? strlen(name) : 0) > HOSTLEN)
1394 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1395 /* Added a check against the user's IP address as well.
1396 * If the line is either CONF_KILL or CONF_IPKILL, check it; if and only
1397 * if it's CONF_IPKILL, check the IP address as well (the && below will
1398 * short circuit and the match won't even get run) -Kev
1400 if ((tmp->status & CONF_KLINE) && tmp->host && tmp->name &&
1401 (match(tmp->host, host) == 0 ||
1402 ((tmp->status == CONF_IPKILL) &&
1403 match(tmp->host, ircd_ntoa((const char*) &cptr->ip)) == 0)) &&
1404 (!name || match(tmp->name, name) == 0) &&
1405 (!tmp->port || (tmp->port == cptr->listener->port)))
1408 * Can short-circuit evaluation - not taking chances
1409 * because check_time_interval destroys tmp->passwd
1412 if (EmptyString(tmp->passwd))
1414 else if (is_comment(tmp->passwd))
1416 else if (check_time_interval(tmp->passwd, reply))
1421 sendto_one(cptr, reply, me.name, ERR_YOUREBANNEDCREEP, cptr->name);
1423 if (EmptyString(tmp->passwd))
1425 ":%s %d %s :Connection from your host is refused on this server.",
1426 me.name, ERR_YOUREBANNEDCREEP, cptr->name);
1428 if (*tmp->passwd == '"') {
1430 sprintf_irc(sendbuf, ":%s %d %s :%s", me.name, ERR_YOUREBANNEDCREEP,
1431 cptr->name, &tmp->passwd[1]);
1432 sbuf[-1] = '.'; /* Overwrite last quote with a dot */
1433 sendbufto_one(cptr);
1435 else if (*tmp->passwd == '!')
1436 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1438 #ifdef COMMENT_IS_FILE
1439 killcomment(cptr, cptr->name, tmp->passwd);
1441 sendto_one(cptr, ":%s %d %s :%s.", me.name, ERR_YOUREBANNEDCREEP,
1442 cptr->name, tmp->passwd);
1447 /* find active glines */
1448 /* added a check against the user's IP address to find_gline() -Kev */
1449 else if ((agline = find_gline(cptr, NULL)) && GlineIsActive(agline))
1450 sendto_one(cptr, ":%s %d %s :%s.", me.name, ERR_YOUREBANNEDCREEP,
1451 cptr->name, agline->reason);
1453 agline = NULL; /* if a gline was found, it was inactive */
1455 return (tmp ? -1 : (agline ? -2 : 0));
1458 struct MotdItem* read_motd(const char* motdfile)
1460 FBFILE* file = NULL;
1461 struct MotdItem* temp;
1462 struct MotdItem* newmotd;
1463 struct MotdItem* last;
1468 if (NULL == (file = fbopen(motdfile, "r"))) {
1469 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1472 if (-1 == fbstat(&sb, file)) {
1476 newmotd = last = NULL;
1477 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1478 while (fbgets(line, sizeof(line) - 1, file)) {
1479 if ((tmp = (char *)strchr(line, '\n')))
1481 if ((tmp = (char *)strchr(line, '\r')))
1483 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1485 strcpy(temp->line, line);
1499 * Ordinary client access check. Look for conf lines which have the same
1500 * status as the flags passed.
1502 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1508 if ((acr = attach_iline(cptr))) {
1509 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1510 cptr->name, cptr->sockhost));
1519 * Check access for a server given its name (passed in cptr struct).
1520 * Must check for all C/N lines which have a name which matches the
1521 * name given and a host which matches. A host alias which is the
1522 * same as the server name is also acceptable in the host field of a
1527 * -1 = Access denied
1530 int conf_check_server(struct Client *cptr)
1532 struct ConfItem* c_conf = NULL;
1535 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1536 cptr->name, cptr->sockhost));
1538 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1539 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1544 * We initiated this connection so the client should have a C and N
1545 * line already attached after passing through the connect_server()
1548 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1549 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1551 sendto_ops("Connect Error: lost C:line for %s", cptr->name);
1552 det_confs_butmask(cptr, 0);
1560 if (cptr->dns_reply) {
1562 struct hostent* hp = cptr->dns_reply->hp;
1563 const char* name = hp->h_name;
1565 * If we are missing a C or N line from above, search for
1566 * it under all known hostnames we have for this ip#.
1568 for (i = 0; name; name = hp->h_aliases[i++]) {
1569 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1570 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1575 for (i = 0; hp->h_addr_list[i]; i++) {
1576 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1583 * Check for C lines with the hostname portion the ip number
1584 * of the host the server runs on. This also checks the case where
1585 * there is a server connecting from 'localhost'.
1587 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1591 * Attach by IP# only if all other checks have failed.
1592 * It is quite possible to get here with the strange things that can
1593 * happen when using DNS in the way the irc server does. -avalon
1596 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1598 * detach all conf lines that got attached by attach_confs()
1600 det_confs_butmask(cptr, 0);
1602 * if no C or no N lines, then deny access
1605 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1606 cptr->name, cptr->username, cptr->sockhost));
1609 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1611 * attach the C and N lines to the client structure for later use.
1613 attach_conf(cptr, c_conf);
1614 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1616 if (INADDR_NONE == c_conf->ipnum.s_addr)
1617 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1619 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));