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 * is the K line field an interval or a comment? - Mmmm
81 static int is_comment(const char *comment)
84 unsigned int len = strlen(comment);
85 for (i = 0; i < len; ++i) {
86 if (!IsKTimeChar(comment[i]))
93 * check against a set of time intervals
95 static int check_time_interval(char *interval, char *reply, size_t reply_size)
100 int perm_min_minutes;
102 int perm_max_minutes;
107 tptr = localtime(&CurrentTime);
108 nowm = tptr->tm_hour * 60 + tptr->tm_min;
111 p = strchr(interval, ',');
114 if (sscanf(interval, "%2d%2d-%2d%2d", &perm_min_hours, &perm_min_minutes,
115 &perm_max_hours, &perm_max_minutes) != 4)
123 perm_min = 60 * perm_min_hours + perm_min_minutes;
124 perm_max = 60 * perm_max_hours + perm_max_minutes;
126 * The following check allows intervals over midnight ...
128 if ((perm_min < perm_max)
129 ? (perm_min <= nowm && nowm <= perm_max)
130 : (perm_min <= nowm || nowm <= perm_max))
132 ircd_snprintf(0, reply, reply_size, ":You are not allowed to connect "
133 "from %d:%02d to %d:%02d.", perm_min_hours,
134 perm_min_minutes, perm_max_hours, perm_max_minutes);
135 return (ERR_YOUREBANNEDCREEP);
137 if ((perm_min < perm_max)
138 ? (perm_min <= nowm + 5 && nowm + 5 <= perm_max)
139 : (perm_min <= nowm + 5 || nowm + 5 <= perm_max))
141 ircd_snprintf(0, reply, reply_size, ":%d minute%s and you will be "
142 "denied for further access", perm_min - nowm,
143 (perm_min - nowm) > 1 ? "s" : "");
144 return (ERR_YOUWILLBEBANNED);
152 * output the reason for being k lined from a file - Mmmm
154 * parv is the sender prefix
155 * filename is the file that is to be output to the K lined client
157 static void killcomment(struct Client *sptr, char *parv, char *filename)
165 if (NULL == (file = fbopen(filename, "r"))) {
166 send_reply(sptr, ERR_NOMOTD);
167 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
168 ":Connection from your host is refused on this server.");
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')))
178 send_reply(sptr, RPL_MOTD, line);
180 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
181 ":Connection from your host is refused on this server.");
185 struct ConfItem* make_conf(void)
187 struct ConfItem* aconf;
189 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
194 aconf->status = CONF_ILLEGAL;
196 aconf->ipnum.s_addr = INADDR_NONE;
202 aconf->dns_pending = 0;
203 aconf->confClass = 0;
208 void delist_conf(struct ConfItem *aconf)
210 if (aconf == GlobalConfList)
211 GlobalConfList = GlobalConfList->next;
213 struct ConfItem *bconf;
215 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
217 bconf->next = aconf->next;
222 void free_conf(struct ConfItem *aconf)
224 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
225 aconf->host ? aconf->host : "*",
226 aconf->name ? aconf->name : "*",
228 if (aconf->dns_pending)
229 delete_resolver_queries(aconf);
232 memset(aconf->passwd, 0, strlen(aconf->passwd));
233 MyFree(aconf->passwd);
242 * conf_dns_callback - called when resolver query finishes
243 * if the query resulted in a successful search, hp will contain
244 * a non-null pointer, otherwise hp will be null.
245 * if successful save hp in the conf item it was called with
247 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
249 struct ConfItem* aconf = (struct ConfItem*) vptr;
250 aconf->dns_pending = 0;
252 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
256 * conf_dns_lookup - do a nameserver lookup of the conf host
257 * if the conf entry is currently doing a ns lookup do nothing, otherwise
258 * if the lookup returns a null pointer, set the conf dns_pending flag
260 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
262 struct DNSReply* dns_reply = 0;
263 if (!aconf->dns_pending) {
264 char buf[HOSTLEN + 1];
265 struct DNSQuery query;
267 query.callback = conf_dns_callback;
268 host_from_uh(buf, aconf->host, HOSTLEN);
271 if (0 == (dns_reply = gethost_byname(buf, &query)))
272 aconf->dns_pending = 1;
281 * Do (start) DNS lookups of all hostnames in the conf line and convert
282 * an IP addresses in a.b.c.d number for to IP#s.
284 static void lookup_confhost(struct ConfItem *aconf)
286 struct DNSReply* reply;
288 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
289 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
290 aconf->host, aconf->name));
294 * Do name lookup now on hostnames given and store the
295 * ip numbers in conf structure.
297 if (IsDigit(*aconf->host)) {
299 * rfc 1035 sez host names may not start with a digit
300 * XXX - this has changed code needs to be updated
302 aconf->ipnum.s_addr = inet_addr(aconf->host);
303 if (INADDR_NONE == aconf->ipnum.s_addr) {
304 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
305 aconf->host, aconf->name));
308 else if ((reply = conf_dns_lookup(aconf)))
309 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
313 * conf_find_server - find a server by name or hostname
314 * returns a server conf item pointer if found, 0 otherwise
316 * NOTE: at some point when we don't have to scan the entire
317 * list it may be cheaper to look for server names and host
318 * names in separate loops (original code did it that way)
320 struct ConfItem* conf_find_server(const char* name)
322 struct ConfItem* conf;
325 for (conf = GlobalConfList; conf; conf = conf->next) {
326 if (CONF_SERVER == conf->status) {
328 * Check first servernames, then try hostnames.
329 * XXX - match returns 0 if there _is_ a match... guess they
330 * haven't decided what true is yet
332 if (0 == match(name, conf->name))
340 * conf_eval_crule - evaluate connection rules
341 * returns the name of the rule triggered if found, 0 otherwise
343 * Evaluate connection rules... If no rules found, allow the
344 * connect. Otherwise stop with the first true rule (ie: rules
345 * are ored together. Oper connects are effected only by D
346 * lines (CRULEALL) not d lines (CRULEAUTO).
348 const char* conf_eval_crule(struct ConfItem* conf)
350 struct ConfItem* rule;
353 for (rule = GlobalConfList; rule; rule = rule->next) {
354 if ((CONF_CRULEALL == rule->status) && (0 == match(rule->host, conf->name))) {
355 if (crule_eval(rule->passwd))
364 * field breakup for ircd.conf file.
366 static char* getfield(char* newline, char fs)
368 static char* gfline = NULL;
378 end = field = gfline;
394 } while (end && fs != ':' && *++end != ':' && *end != '\n') ;
398 if ((end = strchr(field, '\n')) == NULL)
399 end = field + strlen(field);
410 * Remove all conf entries from the client except those which match
411 * the status field mask.
413 void det_confs_butmask(struct Client *cptr, int mask)
415 struct SLink *tmp, *tmp2;
417 for (tmp = cptr->confs; tmp; tmp = tmp2) {
419 if ((tmp->value.aconf->status & mask) == 0)
420 detach_conf(cptr, tmp->value.aconf);
425 * validate_hostent - make sure hostnames are valid in a hostent struct
426 * XXX - this is terrible, what's worse is it used to be in the inner
427 * loop of scanning all the I:lines --Bleep
429 static int validate_hostent(struct hostent* hp)
431 char fullname[HOSTLEN + 1];
436 for (hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
437 unsigned int fullnamelen = 0;
438 unsigned int label_count = 0;
440 ircd_strncpy(fullname, hname, HOSTLEN);
441 fullname[HOSTLEN] = '\0';
443 * Disallow a hostname label to contain anything but a [-a-zA-Z0-9].
444 * It may not start or end on a '.'.
445 * A label may not end on a '-', the maximum length of a label is
447 * On top of that (which seems to be the RFC) we demand that the
448 * top domain does not contain any digits.
450 error = (*hname == '.') ? 1 : 0; /* May not start with a '.' */
453 for (p = fullname; *p; ++p, ++fullnamelen) {
455 /* Label may not end on '-' and May not end on a '.' */
456 if (p[-1] == '-' || p[1] == 0) {
461 error = 0; /* Was not top domain */
464 if (++label_count > 63) {
465 /* Label not longer then 63 */
469 if (*p >= '0' && *p <= '9') {
470 /* In case this is top domain */
474 if (!(*p >= 'a' && *p <= 'z')
475 && !(*p >= 'A' && *p <= 'Z') && *p != '-') {
488 * check_limit_and_attach - check client limits and attach I:line
490 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
491 * and dislallow more than 255 people here as well as in ipcheck.
492 * removed the old "ONE" scheme too.
493 * -- Isomer 2000-06-22
495 static enum AuthorizationCheckResult
496 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
501 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
502 number = *aconf->passwd-'0';
503 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
505 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
507 if (ip_registry_count(cptr->ip.s_addr) > number)
508 return ACR_TOO_MANY_FROM_IP;
509 return attach_conf(cptr, aconf);
513 * Find the first (best) I line to attach.
515 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
517 struct ConfItem* aconf;
520 static char uhost[HOSTLEN + USERLEN + 3];
521 static char fullname[HOSTLEN + 1];
522 struct hostent* hp = 0;
524 if (cptr->dns_reply) {
525 hp = cptr->dns_reply->hp;
526 if (!validate_hostent(hp))
529 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
530 if (aconf->status != CONF_CLIENT)
532 if (aconf->port && aconf->port != cptr->listener->port)
534 if (!aconf->host || !aconf->name)
537 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
538 ircd_strncpy(fullname, hname, HOSTLEN);
539 fullname[HOSTLEN] = '\0';
541 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
543 if (strchr(aconf->name, '@')) {
544 strcpy(uhost, cptr->username);
549 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
550 uhost[sizeof(uhost) - 1] = 0;
551 if (0 == match(aconf->name, uhost)) {
552 if (strchr(uhost, '@'))
553 cptr->flags |= FLAGS_DOID;
554 return check_limit_and_attach(cptr, aconf);
558 if (strchr(aconf->host, '@')) {
559 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
560 uhost[sizeof(uhost) - 2] = 0;
565 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
566 uhost[sizeof(uhost) - 1] = 0;
567 if (match(aconf->host, uhost))
569 if (strchr(uhost, '@'))
570 cptr->flags |= FLAGS_DOID;
572 return check_limit_and_attach(cptr, aconf);
574 return ACR_NO_AUTHORIZATION;
578 * detach_conf - Disassociate configuration from the client.
580 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
587 assert(0 < aconf->clients);
592 if ((*lp)->value.aconf == aconf) {
593 if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) &&
594 ConfLinks(aconf) > 0)
596 if (0 == --aconf->clients && IsIllegal(aconf))
609 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
613 for (lp = cptr->confs; lp; lp = lp->next)
614 if (lp->value.aconf == aconf)
623 * Associate a specific configuration entry to a *local*
624 * client (this is the one which used in accepting the
625 * connection). Note, that this automaticly changes the
626 * attachment if there was an old one...
628 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
632 if (is_attached(aconf, cptr))
633 return ACR_ALREADY_AUTHORIZED;
634 if (IsIllegal(aconf))
635 return ACR_NO_AUTHORIZATION;
636 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
637 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
638 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
640 lp->next = cptr->confs;
641 lp->value.aconf = aconf;
644 if (aconf->status & CONF_CLIENT_MASK)
649 struct ConfItem *find_admin(void)
651 struct ConfItem *aconf;
653 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
654 if (aconf->status & CONF_ADMIN)
660 struct ConfItem* find_me(void)
662 struct ConfItem* aconf;
663 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
664 if (aconf->status & CONF_ME)
671 * attach_confs_byname
673 * Attach a CONF line to a client if the name passed matches that for
674 * the conf file (for non-C/N lines) or is an exact match (C/N lines
675 * only). The difference in behaviour is to stop C:*::* and N:*::*.
677 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
680 struct ConfItem* tmp;
681 struct ConfItem* first = NULL;
685 if (HOSTLEN < strlen(name))
688 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
689 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
690 assert(0 != tmp->name);
691 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
692 if (ACR_OK == attach_conf(cptr, tmp) && !first)
701 * Added for new access check meLazy
703 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
706 struct ConfItem* tmp;
707 struct ConfItem* first = 0;
710 if (HOSTLEN < strlen(host))
713 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
714 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
715 assert(0 != tmp->host);
716 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
717 if (ACR_OK == attach_conf(cptr, tmp) && !first)
726 * find a conf entry which matches the hostname and has the same name.
728 struct ConfItem* find_conf_exact(const char* name, const char* user,
729 const char* host, int statmask)
731 struct ConfItem *tmp;
732 char userhost[USERLEN + HOSTLEN + 3];
735 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
737 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
739 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
740 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
741 0 != ircd_strcmp(tmp->name, name))
744 * Accept if the *real* hostname (usually sockecthost)
745 * socket host) matches *either* host or name field
746 * of the configuration.
748 if (match(tmp->host, userhost))
750 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
751 if (tmp->clients < MaxLinks(tmp->confClass))
762 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
765 struct ConfItem* tmp;
768 if (HOSTLEN < strlen(name))
771 for (; lp; lp = lp->next) {
772 tmp = lp->value.aconf;
773 if (0 != (tmp->status & statmask)) {
774 assert(0 != tmp->name);
775 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
783 * Added for new access check meLazy
785 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
788 struct ConfItem* tmp = NULL;
791 if (HOSTLEN < strlen(host))
794 for (; lp; lp = lp->next) {
795 tmp = lp->value.aconf;
796 if (0 != (tmp->status & statmask)) {
797 assert(0 != tmp->host);
798 if (0 == match(tmp->host, host))
808 * Find a conf line using the IP# stored in it to search upon.
809 * Added 1/8/92 by Avalon.
811 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
814 struct ConfItem* tmp;
816 for (; lp; lp = lp->next) {
817 tmp = lp->value.aconf;
818 if (0 != (tmp->status & statmask)) {
819 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
829 * - looks for a match on all given fields.
831 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
834 struct ConfItem *bconf;
837 mask &= ~CONF_ILLEGAL;
839 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
840 if (!(bconf->status & mask) || (bconf->port != aconf->port))
843 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
844 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
846 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
849 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
850 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
852 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
854 && 0 != ircd_strcmp(bconf->passwd, "ONE")
856 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
859 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
860 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
862 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
872 * Actual REHASH service routine. Called with sig == 0 if it has been called
873 * as a result of an operator issuing this command, else assume it has been
874 * called as a result of the server receiving a HUP signal.
876 int rehash(struct Client *cptr, int sig)
878 struct ConfItem** tmp = &GlobalConfList;
879 struct ConfItem* tmp2;
880 struct ConfClass* cltmp;
881 struct Client* acptr;
882 struct MotdItem* temp;
888 sendto_opmask_butone(0, SNO_OLDSNO,
889 "Got signal SIGHUP, reloading ircd conf. file");
891 while ((tmp2 = *tmp)) {
894 * Configuration entry is still in use by some
895 * local clients, cannot delete it--mark it so
896 * that it will be deleted when the last client
899 if (!(tmp2->status & CONF_CLIENT)) {
905 tmp2->status |= CONF_ILLEGAL;
909 /* free expression trees of connect rules */
910 if ((tmp2->status & (CONF_CRULEALL | CONF_CRULEAUTO)) &&
911 (tmp2->passwd != NULL))
912 crule_free(&(tmp2->passwd));
918 * We don't delete the class table, rather mark all entries
919 * for deletion. The table is cleaned up by check_class(). - avalon
921 for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
925 * delete the juped nicks list
930 flush_resolver_cache();
932 mark_listeners_closing();
934 if (!conf_init()) /* This calls check_class(), */
935 check_class(); /* unless it fails */
938 * make sure that the server listener is re-added so it doesn't get
944 * Flush out deleted I and P lines although still in use.
946 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
947 if (!(tmp2->status & CONF_ILLEGAL))
957 for (i = 0; i <= HighestFd; i++) {
958 if ((acptr = LocalClientArray[i])) {
959 assert(!IsMe(acptr));
960 if (IsServer(acptr)) {
961 det_confs_butmask(acptr,
962 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
963 attach_confs_byname(acptr, acptr->name,
964 CONF_HUB | CONF_LEAF | CONF_UWORLD);
966 /* Because admin's are getting so uppity about people managing to
967 * get past K/G's etc, we'll "fix" the bug by actually explaining
970 if ((found_g = find_kill(acptr))) {
971 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
972 found_g == -2 ? "G-line active for %s%s" :
973 "K-line active for %s%s",
974 IsUnknown(acptr) ? "Unregistered Client ":"",
975 get_client_name(acptr, HIDE_IP));
976 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
977 "K-lined") == CPTR_KILLED)
983 * free old motd structs
995 /* reload motd files */
997 rmotd = read_motd(RPATH);
998 motd = read_motd(MPATH);
1005 * Read configuration file.
1007 * returns 0, if file cannot be opened
1011 #define MAXCONFLINKS 150
1016 static char quotes[9][2] = {
1031 struct ConfItem *aconf = 0;
1033 Debug((DEBUG_DEBUG, "conf_init: ircd.conf = %s", configfile));
1034 if (0 == (file = fbopen(configfile, "r"))) {
1037 while (fbgets(line, sizeof(line) - 1, file)) {
1038 if ((tmp = strchr(line, '\n')))
1041 * Do quoting of characters and # detection.
1043 for (tmp = line; *tmp; tmp++) {
1045 for (i = 0; quotes[i][0]; i++) {
1046 if (quotes[i][0] == *(tmp + 1)) {
1047 *tmp = quotes[i][1];
1056 for (s = tmp; (*s = *(s + 1)); s++)
1060 else if (*tmp == '#')
1063 if (!*line || line[0] == '#' || line[0] == '\n' ||
1064 line[0] == ' ' || line[0] == '\t')
1066 /* Could we test if it's conf line at all? -Vesa */
1067 if (line[1] != ':') {
1068 Debug((DEBUG_ERROR, "Bad config line: %s", line));
1073 aconf = make_conf();
1075 tmp = getfield(line, ':');
1079 case 'A': /* Name, e-mail address of administrator */
1080 case 'a': /* of this server. */
1081 aconf->status = CONF_ADMIN;
1083 case 'C': /* Server where I should try to connect */
1084 case 'c': /* in case of lp failures */
1086 aconf->status = CONF_SERVER;
1090 aconf->status = CONF_CRULEALL;
1092 /* Connect rule - autos only */
1094 aconf->status = CONF_CRULEAUTO;
1096 case 'H': /* Hub server line */
1098 aconf->status = CONF_HUB;
1100 case 'I': /* Just plain normal irc client trying */
1101 case 'i': /* to connect me */
1102 aconf->status = CONF_CLIENT;
1104 case 'K': /* Kill user line on irc.conf */
1105 aconf->status = CONF_KILL;
1107 case 'k': /* Kill user line based on IP in ircd.conf */
1108 aconf->status = CONF_IPKILL;
1110 /* Operator. Line should contain at least */
1111 /* password and host where connection is */
1112 case 'L': /* guaranteed leaf server */
1114 aconf->status = CONF_LEAF;
1116 /* Me. Host field is name used for this host */
1117 /* and port number is the number of the port */
1120 aconf->status = CONF_ME;
1123 aconf->status = CONF_OPERATOR;
1125 /* Local Operator, (limited privs --SRB) */
1127 aconf->status = CONF_LOCOP;
1129 case 'P': /* listen port line */
1131 aconf->status = CONF_LISTEN_PORT;
1133 case 'T': /* print out different motd's */
1134 case 't': /* based on hostmask */
1135 aconf->status = CONF_TLINES;
1137 case 'U': /* Underworld server, allowed to hack modes */
1138 case 'u': /* *Every* server on the net must define the same !!! */
1139 aconf->status = CONF_UWORLD;
1143 aconf->status = CONF_CLASS;
1146 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1149 if (IsIllegal(aconf))
1152 for (;;) { /* Fake loop, that I can use break here --msa */
1153 if ((tmp = getfield(NULL, ':')) == NULL)
1155 DupString(aconf->host, tmp);
1156 if ((tmp = getfield(NULL, (aconf->status == CONF_KILL
1157 || aconf->status == CONF_IPKILL) ? '"' : ':')) == NULL)
1159 DupString(aconf->passwd, tmp);
1160 if ((tmp = getfield(NULL, ':')) == NULL)
1162 DupString(aconf->name, tmp);
1163 if ((tmp = getfield(NULL, ':')) == NULL)
1165 aconf->port = atoi(tmp);
1166 tmp = getfield(NULL, ':');
1167 if (aconf->status & CONF_ME) {
1169 Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1170 "assigned to you by routing-com, behind the port number!\n"));
1171 ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1172 "assigned to you by routing-com, behind the port number!\n");
1175 SetYXXServerName(&me, atoi(tmp)); /* Our Numeric Nick */
1178 aconf->confClass = find_class(atoi(tmp));
1182 * If conf line is a class definition, create a class entry
1183 * for it and make the conf_line illegal and delete it.
1185 if (aconf->status & CONF_CLASS) {
1186 add_class(atoi(aconf->host), atoi(aconf->passwd),
1187 atoi(aconf->name), aconf->port, tmp ? atoi(tmp) : 0);
1191 * Associate each conf line with a class by using a pointer
1192 * to the correct class record. -avalon
1194 if (aconf->status & CONF_CLIENT_MASK) {
1195 if (aconf->confClass == 0)
1196 aconf->confClass = find_class(0);
1198 if (aconf->status & CONF_LISTEN_PORT) {
1201 if (!EmptyString(aconf->name)) {
1202 const char* x = aconf->name;
1203 if ('S' == ToUpper(*x))
1206 if ('H' == ToUpper(*x))
1209 add_listener(aconf->port, aconf->passwd, aconf->host,
1210 is_server, is_hidden);
1213 if (aconf->status & CONF_CLIENT) {
1214 struct ConfItem *bconf;
1216 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1218 bconf->status &= ~CONF_ILLEGAL;
1219 if (aconf->status == CONF_CLIENT) {
1221 * copy the password field in case it changed
1223 MyFree(bconf->passwd);
1224 bconf->passwd = aconf->passwd;
1227 ConfLinks(bconf) -= bconf->clients;
1228 bconf->confClass = aconf->confClass;
1229 if (bconf->confClass)
1230 ConfLinks(bconf) += bconf->clients;
1236 if (aconf->status & CONF_SERVER) {
1237 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1238 strchr(aconf->host, '?') || !aconf->name)
1241 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1242 if (!strchr(aconf->host, '@')) {
1244 int len = 3; /* *@\0 = 3 */
1246 len += strlen(aconf->host);
1247 newhost = (char*) MyMalloc(len);
1248 assert(0 != newhost);
1249 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1250 MyFree(aconf->host);
1251 aconf->host = newhost;
1254 if (aconf->status & CONF_SERVER) {
1255 if (EmptyString(aconf->passwd))
1257 lookup_confhost(aconf);
1260 /* Create expression tree from connect rule...
1261 * If there's a parsing error, nuke the conf structure */
1262 if (aconf->status & (CONF_CRULEALL | CONF_CRULEAUTO)) {
1263 MyFree(aconf->passwd);
1264 if ((aconf->passwd = (char *)crule_parse(aconf->name)) == NULL) {
1272 * Own port and name cannot be changed after the startup.
1273 * (or could be allowed, but only if all links are closed first).
1274 * Configuration info does not override the name and port
1275 * if previously defined. Note, that "info"-field can be
1276 * changed by "/rehash".
1278 if (aconf->status == CONF_ME) {
1279 ircd_strncpy(me.info, aconf->name, REALLEN);
1283 * Juped nicks are listed in the 'password' field of U:lines,
1284 * the list is comma separated and might be empty and/or contain
1285 * empty elements... the only limit is that it MUST be shorter
1286 * than 512 chars, or they will be cutted out :)
1288 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1289 addNickJupes(aconf->passwd);
1291 if (aconf->status & CONF_ADMIN) {
1292 if (!aconf->host || !aconf->passwd || !aconf->name) {
1293 Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
1294 ircd_log(L_WARNING, "Your A: line must have 4 fields!\n");
1298 collapse(aconf->host);
1299 collapse(aconf->name);
1300 Debug((DEBUG_NOTICE,
1301 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1302 aconf->status, aconf->host, aconf->passwd,
1303 aconf->name, aconf->port, aconf->confClass));
1304 aconf->next = GlobalConfList;
1305 GlobalConfList = aconf;
1312 nextping = nextconnect = CurrentTime;
1317 * Read info from T:lines into TRecords which include the file
1318 * timestamp, the hostmask, and the contents of the motd file
1323 struct ConfItem *tmp;
1324 struct TRecord *temp;
1325 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1326 struct MotdItem *amotd;
1328 /* Free the old trecords and the associated motd contents first */
1332 while (tdata->tmotd)
1334 amotd = tdata->tmotd->next;
1335 MyFree(tdata->tmotd);
1336 tdata->tmotd = amotd;
1342 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1343 if (tmp->status == CONF_TLINES && tmp->host && tmp->passwd) {
1344 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1347 temp->hostmask = tmp->host;
1348 temp->tmotd = read_motd(tmp->passwd);
1349 temp->tmotd_tm = motd_tm;
1360 int find_kill(struct Client *cptr)
1365 struct ConfItem* tmp;
1366 struct Gline* agline = NULL;
1373 host = cptr->sockhost;
1374 name = cptr->user->username;
1379 * XXX - if this ever happens, we're already screwed
1381 if (strlen(host) > HOSTLEN ||
1382 (name ? strlen(name) : 0) > HOSTLEN)
1388 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1389 /* Added a check against the user's IP address as well.
1390 * If the line is either CONF_KILL or CONF_IPKILL, check it; if and only
1391 * if it's CONF_IPKILL, check the IP address as well (the && below will
1392 * short circuit and the match won't even get run) -Kev
1394 if ((tmp->status & CONF_KLINE) && tmp->host && tmp->name &&
1395 (match(tmp->host, host) == 0 ||
1396 ((tmp->status == CONF_IPKILL) &&
1397 match(tmp->host, ircd_ntoa((const char*) &cptr->ip)) == 0)) &&
1398 (!name || match(tmp->name, name) == 0) &&
1399 (!tmp->port || (tmp->port == cptr->listener->port)))
1402 * Can short-circuit evaluation - not taking chances
1403 * because check_time_interval destroys tmp->passwd
1406 if (EmptyString(tmp->passwd))
1408 else if (is_comment(tmp->passwd))
1410 else if (check_time_interval(tmp->passwd, reply, sizeof(reply)))
1415 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, reply);
1417 if (EmptyString(tmp->passwd))
1418 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1419 ":Connection from your host is refused on this server.");
1421 if (*tmp->passwd == '"') {
1422 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1423 strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1425 else if (*tmp->passwd == '!')
1426 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1428 #ifdef COMMENT_IS_FILE
1429 killcomment(cptr, cptr->name, tmp->passwd);
1431 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1437 /* find active glines */
1438 /* added a check against the user's IP address to find_gline() -Kev */
1439 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1440 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1441 GlineReason(agline));
1443 agline = NULL; /* if a gline was found, it was inactive */
1445 return (tmp ? -1 : (agline ? -2 : 0));
1448 struct MotdItem* read_motd(const char* motdfile)
1450 FBFILE* file = NULL;
1451 struct MotdItem* temp;
1452 struct MotdItem* newmotd;
1453 struct MotdItem* last;
1458 if (NULL == (file = fbopen(motdfile, "r"))) {
1459 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1462 if (-1 == fbstat(&sb, file)) {
1466 newmotd = last = NULL;
1467 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1468 while (fbgets(line, sizeof(line) - 1, file)) {
1469 if ((tmp = (char *)strchr(line, '\n')))
1471 if ((tmp = (char *)strchr(line, '\r')))
1473 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1475 strcpy(temp->line, line);
1489 * Ordinary client access check. Look for conf lines which have the same
1490 * status as the flags passed.
1492 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1494 enum AuthorizationCheckResult acr = ACR_OK;
1498 if ((acr = attach_iline(cptr))) {
1499 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1500 cptr->name, cptr->sockhost));
1509 * Check access for a server given its name (passed in cptr struct).
1510 * Must check for all C/N lines which have a name which matches the
1511 * name given and a host which matches. A host alias which is the
1512 * same as the server name is also acceptable in the host field of a
1517 * -1 = Access denied
1520 int conf_check_server(struct Client *cptr)
1522 struct ConfItem* c_conf = NULL;
1525 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1526 cptr->name, cptr->sockhost));
1528 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1529 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1534 * We initiated this connection so the client should have a C and N
1535 * line already attached after passing through the connect_server()
1538 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1539 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1541 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1543 det_confs_butmask(cptr, 0);
1551 if (cptr->dns_reply) {
1553 struct hostent* hp = cptr->dns_reply->hp;
1554 const char* name = hp->h_name;
1556 * If we are missing a C or N line from above, search for
1557 * it under all known hostnames we have for this ip#.
1559 for (i = 0; name; name = hp->h_aliases[i++]) {
1560 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1561 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1566 for (i = 0; hp->h_addr_list[i]; i++) {
1567 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1574 * Check for C lines with the hostname portion the ip number
1575 * of the host the server runs on. This also checks the case where
1576 * there is a server connecting from 'localhost'.
1578 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1582 * Attach by IP# only if all other checks have failed.
1583 * It is quite possible to get here with the strange things that can
1584 * happen when using DNS in the way the irc server does. -avalon
1587 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1589 * detach all conf lines that got attached by attach_confs()
1591 det_confs_butmask(cptr, 0);
1593 * if no C or no N lines, then deny access
1596 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1597 cptr->name, cptr->username, cptr->sockhost));
1600 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1602 * attach the C and N lines to the client structure for later use.
1604 attach_conf(cptr, c_conf);
1605 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1607 if (INADDR_NONE == c_conf->ipnum.s_addr)
1608 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1610 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));