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 static enum AuthorizationCheckResult
491 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
494 /* Special case: exactly one digit */
495 if (IsDigit(*aconf->passwd) && !aconf->passwd[1]) {
497 * Refuse connections when there are already <digit>
498 * clients connected with the same IP number
500 unsigned short nr = *aconf->passwd - '0';
501 if (ip_registry_count(cptr->ip.s_addr) > nr)
502 return ACR_TOO_MANY_FROM_IP; /* Already got nr with that ip# */
505 else if (0 == strcmp(aconf->passwd, "ONE")) {
507 for (i = HighestFd; i > -1; --i) {
508 if (LocalClientArray[i] && MyUser(LocalClientArray[i]) &&
509 LocalClientArray[i]->ip.s_addr == cptr->ip.s_addr)
510 return ACR_TOO_MANY_FROM_IP; /* Already got one with that ip# */
515 return attach_conf(cptr, aconf);
519 * Find the first (best) I line to attach.
521 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
523 struct ConfItem* aconf;
526 static char uhost[HOSTLEN + USERLEN + 3];
527 static char fullname[HOSTLEN + 1];
528 struct hostent* hp = 0;
530 if (cptr->dns_reply) {
531 hp = cptr->dns_reply->hp;
532 if (!validate_hostent(hp))
535 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
536 if (aconf->status != CONF_CLIENT)
538 if (aconf->port && aconf->port != cptr->listener->port)
540 if (!aconf->host || !aconf->name)
543 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
544 ircd_strncpy(fullname, hname, HOSTLEN);
545 fullname[HOSTLEN] = '\0';
547 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
549 if (strchr(aconf->name, '@')) {
550 strcpy(uhost, cptr->username);
555 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
556 uhost[sizeof(uhost) - 1] = 0;
557 if (0 == match(aconf->name, uhost)) {
558 if (strchr(uhost, '@'))
559 cptr->flags |= FLAGS_DOID;
560 return check_limit_and_attach(cptr, aconf);
564 if (strchr(aconf->host, '@')) {
565 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
566 uhost[sizeof(uhost) - 2] = 0;
571 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
572 uhost[sizeof(uhost) - 1] = 0;
573 if (match(aconf->host, uhost))
575 if (strchr(uhost, '@'))
576 cptr->flags |= FLAGS_DOID;
578 return check_limit_and_attach(cptr, aconf);
580 return ACR_NO_AUTHORIZATION;
584 * detach_conf - Disassociate configuration from the client.
586 int detach_conf(struct Client *cptr, struct ConfItem *aconf)
593 assert(0 < aconf->clients);
598 if ((*lp)->value.aconf == aconf) {
599 if (aconf->confClass && (aconf->status & CONF_CLIENT_MASK) &&
600 ConfLinks(aconf) > 0)
602 if (0 == --aconf->clients && IsIllegal(aconf))
615 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
619 for (lp = cptr->confs; lp; lp = lp->next)
620 if (lp->value.aconf == aconf)
629 * Associate a specific configuration entry to a *local*
630 * client (this is the one which used in accepting the
631 * connection). Note, that this automaticly changes the
632 * attachment if there was an old one...
634 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
638 if (is_attached(aconf, cptr))
639 return ACR_ALREADY_AUTHORIZED;
640 if (IsIllegal(aconf))
641 return ACR_NO_AUTHORIZATION;
642 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
643 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
644 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
646 lp->next = cptr->confs;
647 lp->value.aconf = aconf;
650 if (aconf->status & CONF_CLIENT_MASK)
655 struct ConfItem *find_admin(void)
657 struct ConfItem *aconf;
659 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
660 if (aconf->status & CONF_ADMIN)
666 struct ConfItem* find_me(void)
668 struct ConfItem* aconf;
669 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
670 if (aconf->status & CONF_ME)
677 * attach_confs_byname
679 * Attach a CONF line to a client if the name passed matches that for
680 * the conf file (for non-C/N lines) or is an exact match (C/N lines
681 * only). The difference in behaviour is to stop C:*::* and N:*::*.
683 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
686 struct ConfItem* tmp;
687 struct ConfItem* first = NULL;
691 if (HOSTLEN < strlen(name))
694 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
695 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
696 assert(0 != tmp->name);
697 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
698 if (ACR_OK == attach_conf(cptr, tmp) && !first)
707 * Added for new access check meLazy
709 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
712 struct ConfItem* tmp;
713 struct ConfItem* first = 0;
716 if (HOSTLEN < strlen(host))
719 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
720 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
721 assert(0 != tmp->host);
722 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
723 if (ACR_OK == attach_conf(cptr, tmp) && !first)
732 * find a conf entry which matches the hostname and has the same name.
734 struct ConfItem* find_conf_exact(const char* name, const char* user,
735 const char* host, int statmask)
737 struct ConfItem *tmp;
738 char userhost[USERLEN + HOSTLEN + 3];
741 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
743 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
745 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
746 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
747 0 != ircd_strcmp(tmp->name, name))
750 * Accept if the *real* hostname (usually sockecthost)
751 * socket host) matches *either* host or name field
752 * of the configuration.
754 if (match(tmp->host, userhost))
756 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
757 if (tmp->clients < MaxLinks(tmp->confClass))
768 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
771 struct ConfItem* tmp;
774 if (HOSTLEN < strlen(name))
777 for (; lp; lp = lp->next) {
778 tmp = lp->value.aconf;
779 if (0 != (tmp->status & statmask)) {
780 assert(0 != tmp->name);
781 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
789 * Added for new access check meLazy
791 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
794 struct ConfItem* tmp;
797 if (HOSTLEN < strlen(host))
800 for (; lp; lp = lp->next) {
801 tmp = lp->value.aconf;
802 if (0 != (tmp->status & statmask)) {
803 assert(0 != tmp->host);
804 if (0 == match(tmp->host, host))
814 * Find a conf line using the IP# stored in it to search upon.
815 * Added 1/8/92 by Avalon.
817 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
820 struct ConfItem* tmp;
822 for (; lp; lp = lp->next) {
823 tmp = lp->value.aconf;
824 if (0 != (tmp->status & statmask)) {
825 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
835 * - looks for a match on all given fields.
837 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
840 struct ConfItem *bconf;
843 mask &= ~CONF_ILLEGAL;
845 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
846 if (!(bconf->status & mask) || (bconf->port != aconf->port))
849 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
850 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
852 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
855 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
856 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
858 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
860 && 0 != ircd_strcmp(bconf->passwd, "ONE")
862 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
865 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
866 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
868 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
878 * Actual REHASH service routine. Called with sig == 0 if it has been called
879 * as a result of an operator issuing this command, else assume it has been
880 * called as a result of the server receiving a HUP signal.
882 int rehash(struct Client *cptr, int sig)
884 struct ConfItem** tmp = &GlobalConfList;
885 struct ConfItem* tmp2;
886 struct ConfClass* cltmp;
887 struct Client* acptr;
888 struct MotdItem* temp;
894 sendto_opmask_butone(0, SNO_OLDSNO,
895 "Got signal SIGHUP, reloading ircd conf. file");
897 while ((tmp2 = *tmp)) {
900 * Configuration entry is still in use by some
901 * local clients, cannot delete it--mark it so
902 * that it will be deleted when the last client
905 if (!(tmp2->status & CONF_CLIENT)) {
911 tmp2->status |= CONF_ILLEGAL;
915 /* free expression trees of connect rules */
916 if ((tmp2->status & (CONF_CRULEALL | CONF_CRULEAUTO)) &&
917 (tmp2->passwd != NULL))
918 crule_free(&(tmp2->passwd));
924 * We don't delete the class table, rather mark all entries
925 * for deletion. The table is cleaned up by check_class(). - avalon
927 for (cltmp = NextClass(FirstClass()); cltmp; cltmp = NextClass(cltmp))
931 * delete the juped nicks list
936 flush_resolver_cache();
938 mark_listeners_closing();
940 if (!conf_init()) /* This calls check_class(), */
941 check_class(); /* unless it fails */
944 * make sure that the server listener is re-added so it doesn't get
950 * Flush out deleted I and P lines although still in use.
952 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
953 if (!(tmp2->status & CONF_ILLEGAL))
963 for (i = 0; i <= HighestFd; i++) {
964 if ((acptr = LocalClientArray[i])) {
965 assert(!IsMe(acptr));
966 if (IsServer(acptr)) {
967 det_confs_butmask(acptr,
968 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
969 attach_confs_byname(acptr, acptr->name,
970 CONF_HUB | CONF_LEAF | CONF_UWORLD);
972 if ((found_g = find_kill(acptr))) {
973 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
974 found_g == -2 ? "G-line active for %s" :
975 "K-line active for %s",
976 get_client_name(acptr, HIDE_IP));
977 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
978 "K-lined") == CPTR_KILLED)
984 * free old motd structs
996 /* reload motd files */
998 rmotd = read_motd(RPATH);
999 motd = read_motd(MPATH);
1006 * Read configuration file.
1008 * returns 0, if file cannot be opened
1012 #define MAXCONFLINKS 150
1017 static char quotes[9][2] = {
1032 struct ConfItem *aconf = 0;
1034 Debug((DEBUG_DEBUG, "conf_init: ircd.conf = %s", configfile));
1035 if (0 == (file = fbopen(configfile, "r"))) {
1038 while (fbgets(line, sizeof(line) - 1, file)) {
1039 if ((tmp = strchr(line, '\n')))
1042 * Do quoting of characters and # detection.
1044 for (tmp = line; *tmp; tmp++) {
1046 for (i = 0; quotes[i][0]; i++) {
1047 if (quotes[i][0] == *(tmp + 1)) {
1048 *tmp = quotes[i][1];
1057 for (s = tmp; (*s = *(s + 1)); s++)
1061 else if (*tmp == '#')
1064 if (!*line || line[0] == '#' || line[0] == '\n' ||
1065 line[0] == ' ' || line[0] == '\t')
1067 /* Could we test if it's conf line at all? -Vesa */
1068 if (line[1] != ':') {
1069 Debug((DEBUG_ERROR, "Bad config line: %s", line));
1074 aconf = make_conf();
1076 tmp = getfield(line, ':');
1080 case 'A': /* Name, e-mail address of administrator */
1081 case 'a': /* of this server. */
1082 aconf->status = CONF_ADMIN;
1084 case 'C': /* Server where I should try to connect */
1085 case 'c': /* in case of lp failures */
1087 aconf->status = CONF_SERVER;
1091 aconf->status = CONF_CRULEALL;
1093 /* Connect rule - autos only */
1095 aconf->status = CONF_CRULEAUTO;
1097 case 'H': /* Hub server line */
1099 aconf->status = CONF_HUB;
1101 case 'I': /* Just plain normal irc client trying */
1102 case 'i': /* to connect me */
1103 aconf->status = CONF_CLIENT;
1105 case 'K': /* Kill user line on irc.conf */
1106 aconf->status = CONF_KILL;
1108 case 'k': /* Kill user line based on IP in ircd.conf */
1109 aconf->status = CONF_IPKILL;
1111 /* Operator. Line should contain at least */
1112 /* password and host where connection is */
1113 case 'L': /* guaranteed leaf server */
1115 aconf->status = CONF_LEAF;
1117 /* Me. Host field is name used for this host */
1118 /* and port number is the number of the port */
1121 aconf->status = CONF_ME;
1124 aconf->status = CONF_OPERATOR;
1126 /* Local Operator, (limited privs --SRB) */
1128 aconf->status = CONF_LOCOP;
1130 case 'P': /* listen port line */
1132 aconf->status = CONF_LISTEN_PORT;
1134 case 'T': /* print out different motd's */
1135 case 't': /* based on hostmask */
1136 aconf->status = CONF_TLINES;
1138 case 'U': /* Underworld server, allowed to hack modes */
1139 case 'u': /* *Every* server on the net must define the same !!! */
1140 aconf->status = CONF_UWORLD;
1144 aconf->status = CONF_CLASS;
1147 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1150 if (IsIllegal(aconf))
1153 for (;;) { /* Fake loop, that I can use break here --msa */
1154 if ((tmp = getfield(NULL, ':')) == NULL)
1156 DupString(aconf->host, tmp);
1157 if ((tmp = getfield(NULL, (aconf->status == CONF_KILL
1158 || aconf->status == CONF_IPKILL) ? '"' : ':')) == NULL)
1160 DupString(aconf->passwd, tmp);
1161 if ((tmp = getfield(NULL, ':')) == NULL)
1163 DupString(aconf->name, tmp);
1164 if ((tmp = getfield(NULL, ':')) == NULL)
1166 aconf->port = atoi(tmp);
1167 tmp = getfield(NULL, ':');
1168 if (aconf->status & CONF_ME) {
1170 Debug((DEBUG_FATAL, "Your M: line must have the Numeric, "
1171 "assigned to you by routing-com, behind the port number!\n"));
1172 ircd_log(L_WARNING, "Your M: line must have the Numeric, "
1173 "assigned to you by routing-com, behind the port number!\n");
1176 SetYXXServerName(&me, atoi(tmp)); /* Our Numeric Nick */
1179 aconf->confClass = find_class(atoi(tmp));
1183 * If conf line is a class definition, create a class entry
1184 * for it and make the conf_line illegal and delete it.
1186 if (aconf->status & CONF_CLASS) {
1187 add_class(atoi(aconf->host), atoi(aconf->passwd),
1188 atoi(aconf->name), aconf->port, tmp ? atoi(tmp) : 0);
1192 * Associate each conf line with a class by using a pointer
1193 * to the correct class record. -avalon
1195 if (aconf->status & CONF_CLIENT_MASK) {
1196 if (aconf->confClass == 0)
1197 aconf->confClass = find_class(0);
1199 if (aconf->status & CONF_LISTEN_PORT) {
1202 if (!EmptyString(aconf->name)) {
1203 const char* x = aconf->name;
1204 if ('S' == ToUpper(*x))
1207 if ('H' == ToUpper(*x))
1210 add_listener(aconf->port, aconf->passwd, aconf->host,
1211 is_server, is_hidden);
1214 if (aconf->status & CONF_CLIENT) {
1215 struct ConfItem *bconf;
1217 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1219 bconf->status &= ~CONF_ILLEGAL;
1220 if (aconf->status == CONF_CLIENT) {
1222 * copy the password field in case it changed
1224 MyFree(bconf->passwd);
1225 bconf->passwd = aconf->passwd;
1228 ConfLinks(bconf) -= bconf->clients;
1229 bconf->confClass = aconf->confClass;
1230 if (bconf->confClass)
1231 ConfLinks(bconf) += bconf->clients;
1237 if (aconf->status & CONF_SERVER) {
1238 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1239 strchr(aconf->host, '?') || !aconf->name)
1242 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1243 if (!strchr(aconf->host, '@')) {
1245 int len = 3; /* *@\0 = 3 */
1247 len += strlen(aconf->host);
1248 newhost = (char*) MyMalloc(len);
1249 assert(0 != newhost);
1250 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1251 MyFree(aconf->host);
1252 aconf->host = newhost;
1255 if (aconf->status & CONF_SERVER) {
1256 if (EmptyString(aconf->passwd))
1258 lookup_confhost(aconf);
1261 /* Create expression tree from connect rule...
1262 * If there's a parsing error, nuke the conf structure */
1263 if (aconf->status & (CONF_CRULEALL | CONF_CRULEAUTO)) {
1264 MyFree(aconf->passwd);
1265 if ((aconf->passwd = (char *)crule_parse(aconf->name)) == NULL) {
1273 * Own port and name cannot be changed after the startup.
1274 * (or could be allowed, but only if all links are closed first).
1275 * Configuration info does not override the name and port
1276 * if previously defined. Note, that "info"-field can be
1277 * changed by "/rehash".
1279 if (aconf->status == CONF_ME) {
1280 ircd_strncpy(me.info, aconf->name, REALLEN);
1284 * Juped nicks are listed in the 'password' field of U:lines,
1285 * the list is comma separated and might be empty and/or contain
1286 * empty elements... the only limit is that it MUST be shorter
1287 * than 512 chars, or they will be cutted out :)
1289 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1290 addNickJupes(aconf->passwd);
1292 if (aconf->status & CONF_ADMIN) {
1293 if (!aconf->host || !aconf->passwd || !aconf->name) {
1294 Debug((DEBUG_FATAL, "Your A: line must have 4 fields!\n"));
1295 ircd_log(L_WARNING, "Your A: line must have 4 fields!\n");
1299 collapse(aconf->host);
1300 collapse(aconf->name);
1301 Debug((DEBUG_NOTICE,
1302 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1303 aconf->status, aconf->host, aconf->passwd,
1304 aconf->name, aconf->port, aconf->confClass));
1305 aconf->next = GlobalConfList;
1306 GlobalConfList = aconf;
1313 nextping = nextconnect = CurrentTime;
1318 * Read info from T:lines into TRecords which include the file
1319 * timestamp, the hostmask, and the contents of the motd file
1324 struct ConfItem *tmp;
1325 struct TRecord *temp;
1326 struct TRecord *last = NULL; /* Init. to avoid compiler warning */
1327 struct MotdItem *amotd;
1329 /* Free the old trecords and the associated motd contents first */
1333 while (tdata->tmotd)
1335 amotd = tdata->tmotd->next;
1336 MyFree(tdata->tmotd);
1337 tdata->tmotd = amotd;
1343 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1344 if (tmp->status == CONF_TLINES && tmp->host && tmp->passwd) {
1345 temp = (struct TRecord*) MyMalloc(sizeof(struct TRecord));
1348 temp->hostmask = tmp->host;
1349 temp->tmotd = read_motd(tmp->passwd);
1350 temp->tmotd_tm = motd_tm;
1361 int find_kill(struct Client *cptr)
1366 struct ConfItem* tmp;
1367 struct Gline* agline = NULL;
1374 host = cptr->sockhost;
1375 name = cptr->user->username;
1380 * XXX - if this ever happens, we're already screwed
1382 if (strlen(host) > HOSTLEN ||
1383 (name ? strlen(name) : 0) > HOSTLEN)
1389 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
1390 /* Added a check against the user's IP address as well.
1391 * If the line is either CONF_KILL or CONF_IPKILL, check it; if and only
1392 * if it's CONF_IPKILL, check the IP address as well (the && below will
1393 * short circuit and the match won't even get run) -Kev
1395 if ((tmp->status & CONF_KLINE) && tmp->host && tmp->name &&
1396 (match(tmp->host, host) == 0 ||
1397 ((tmp->status == CONF_IPKILL) &&
1398 match(tmp->host, ircd_ntoa((const char*) &cptr->ip)) == 0)) &&
1399 (!name || match(tmp->name, name) == 0) &&
1400 (!tmp->port || (tmp->port == cptr->listener->port)))
1403 * Can short-circuit evaluation - not taking chances
1404 * because check_time_interval destroys tmp->passwd
1407 if (EmptyString(tmp->passwd))
1409 else if (is_comment(tmp->passwd))
1411 else if (check_time_interval(tmp->passwd, reply, sizeof(reply)))
1416 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, reply);
1418 if (EmptyString(tmp->passwd))
1419 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1420 ":Connection from your host is refused on this server.");
1422 if (*tmp->passwd == '"') {
1423 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%*s.",
1424 strlen(tmp->passwd + 1) - 1, tmp->passwd + 1);
1426 else if (*tmp->passwd == '!')
1427 killcomment(cptr, cptr->name, &tmp->passwd[1]);
1429 #ifdef COMMENT_IS_FILE
1430 killcomment(cptr, cptr->name, tmp->passwd);
1432 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1438 /* find active glines */
1439 /* added a check against the user's IP address to find_gline() -Kev */
1440 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline))
1441 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.",
1442 GlineReason(agline));
1444 agline = NULL; /* if a gline was found, it was inactive */
1446 return (tmp ? -1 : (agline ? -2 : 0));
1449 struct MotdItem* read_motd(const char* motdfile)
1451 FBFILE* file = NULL;
1452 struct MotdItem* temp;
1453 struct MotdItem* newmotd;
1454 struct MotdItem* last;
1459 if (NULL == (file = fbopen(motdfile, "r"))) {
1460 Debug((DEBUG_ERROR, "Couldn't open \"%s\": %s", motdfile, strerror(errno)));
1463 if (-1 == fbstat(&sb, file)) {
1467 newmotd = last = NULL;
1468 motd_tm = *localtime((time_t *) & sb.st_mtime); /* NetBSD needs cast */
1469 while (fbgets(line, sizeof(line) - 1, file)) {
1470 if ((tmp = (char *)strchr(line, '\n')))
1472 if ((tmp = (char *)strchr(line, '\r')))
1474 temp = (struct MotdItem*) MyMalloc(sizeof(struct MotdItem));
1476 strcpy(temp->line, line);
1490 * Ordinary client access check. Look for conf lines which have the same
1491 * status as the flags passed.
1493 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1495 enum AuthorizationCheckResult acr = ACR_OK;
1499 if ((acr = attach_iline(cptr))) {
1500 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1501 cptr->name, cptr->sockhost));
1510 * Check access for a server given its name (passed in cptr struct).
1511 * Must check for all C/N lines which have a name which matches the
1512 * name given and a host which matches. A host alias which is the
1513 * same as the server name is also acceptable in the host field of a
1518 * -1 = Access denied
1521 int conf_check_server(struct Client *cptr)
1523 struct ConfItem* c_conf = NULL;
1526 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1527 cptr->name, cptr->sockhost));
1529 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1530 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1535 * We initiated this connection so the client should have a C and N
1536 * line already attached after passing through the connect_server()
1539 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1540 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1542 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1544 det_confs_butmask(cptr, 0);
1552 if (cptr->dns_reply) {
1554 struct hostent* hp = cptr->dns_reply->hp;
1555 const char* name = hp->h_name;
1557 * If we are missing a C or N line from above, search for
1558 * it under all known hostnames we have for this ip#.
1560 for (i = 0; name; name = hp->h_aliases[i++]) {
1561 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1562 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1567 for (i = 0; hp->h_addr_list[i]; i++) {
1568 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1575 * Check for C lines with the hostname portion the ip number
1576 * of the host the server runs on. This also checks the case where
1577 * there is a server connecting from 'localhost'.
1579 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1583 * Attach by IP# only if all other checks have failed.
1584 * It is quite possible to get here with the strange things that can
1585 * happen when using DNS in the way the irc server does. -avalon
1588 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1590 * detach all conf lines that got attached by attach_confs()
1592 det_confs_butmask(cptr, 0);
1594 * if no C or no N lines, then deny access
1597 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1598 cptr->name, cptr->username, cptr->sockhost));
1601 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1603 * attach the C and N lines to the client structure for later use.
1605 attach_conf(cptr, c_conf);
1606 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1608 if (INADDR_NONE == c_conf->ipnum.s_addr)
1609 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1611 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));