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"
51 #include "sprintf_irc.h"
57 #include <arpa/inet.h>
68 #define INADDR_NONE 0xffffffff
71 struct ConfItem* GlobalConfList = 0;
72 int GlobalConfCount = 0;
74 static struct LocalConf localConf;
75 static struct CRuleConf* cruleConfList;
76 static struct ServerConf* serverConfList;
77 static struct DenyConf* denyConfList;
80 * output the reason for being k lined from a file - Mmmm
81 * sptr is client being dumped
82 * filename is the file that is to be output to the K lined client
84 static void killcomment(struct Client* sptr, const char* filename)
91 if (NULL == (file = fbopen(filename, "r"))) {
92 send_reply(sptr, ERR_NOMOTD);
93 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
94 ":Connection from your host is refused on this server.");
98 tm = localtime((time_t*) &sb.st_mtime); /* NetBSD needs cast */
99 while (fbgets(line, sizeof(line) - 1, file)) {
100 char* end = line + strlen(line);
103 if ('\n' == *end || '\r' == *end)
108 send_reply(sptr, RPL_MOTD, line);
110 send_reply(sptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
111 ":Connection from your host is refused on this server.");
115 struct ConfItem* make_conf(void)
117 struct ConfItem* aconf;
119 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
124 memset(aconf, 0, sizeof(struct ConfItem));
125 aconf->status = CONF_ILLEGAL;
126 aconf->ipnum.s_addr = INADDR_NONE;
130 void delist_conf(struct ConfItem *aconf)
132 if (aconf == GlobalConfList)
133 GlobalConfList = GlobalConfList->next;
135 struct ConfItem *bconf;
137 for (bconf = GlobalConfList; aconf != bconf->next; bconf = bconf->next)
139 bconf->next = aconf->next;
144 void free_conf(struct ConfItem *aconf)
146 Debug((DEBUG_DEBUG, "free_conf: %s %s %d",
147 aconf->host ? aconf->host : "*",
148 aconf->name ? aconf->name : "*",
150 if (aconf->dns_pending)
151 delete_resolver_queries(aconf);
154 memset(aconf->passwd, 0, strlen(aconf->passwd));
155 MyFree(aconf->passwd);
164 * detach_conf - Disassociate configuration from the client.
166 static void detach_conf(struct Client* cptr, struct ConfItem* aconf)
173 assert(0 < aconf->clients);
178 if ((*lp)->value.aconf == aconf) {
179 if (aconf->conn_class && (aconf->status & CONF_CLIENT_MASK) && ConfLinks(aconf) > 0)
182 assert(0 < aconf->clients);
183 if (0 == --aconf->clients && IsIllegal(aconf))
195 * conf_dns_callback - called when resolver query finishes
196 * if the query resulted in a successful search, hp will contain
197 * a non-null pointer, otherwise hp will be null.
198 * if successful save hp in the conf item it was called with
200 static void conf_dns_callback(void* vptr, struct DNSReply* reply)
202 struct ConfItem* aconf = (struct ConfItem*) vptr;
203 aconf->dns_pending = 0;
205 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
209 * conf_dns_lookup - do a nameserver lookup of the conf host
210 * if the conf entry is currently doing a ns lookup do nothing, otherwise
211 * if the lookup returns a null pointer, set the conf dns_pending flag
213 static struct DNSReply* conf_dns_lookup(struct ConfItem* aconf)
215 struct DNSReply* dns_reply = 0;
216 if (!aconf->dns_pending) {
217 char buf[HOSTLEN + 1];
218 struct DNSQuery query;
220 query.callback = conf_dns_callback;
221 host_from_uh(buf, aconf->host, HOSTLEN);
224 if (0 == (dns_reply = gethost_byname(buf, &query)))
225 aconf->dns_pending = 1;
234 * Do (start) DNS lookups of all hostnames in the conf line and convert
235 * an IP addresses in a.b.c.d number for to IP#s.
237 static void lookup_confhost(struct ConfItem *aconf)
239 struct DNSReply* reply;
241 if (EmptyString(aconf->host) || EmptyString(aconf->name)) {
242 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
243 aconf->host, aconf->name));
247 * Do name lookup now on hostnames given and store the
248 * ip numbers in conf structure.
250 if (IsDigit(*aconf->host)) {
252 * rfc 1035 sez host names may not start with a digit
253 * XXX - this has changed code needs to be updated
255 aconf->ipnum.s_addr = inet_addr(aconf->host);
256 if (INADDR_NONE == aconf->ipnum.s_addr) {
257 Debug((DEBUG_ERROR, "Host/server name error: (%s) (%s)",
258 aconf->host, aconf->name));
261 else if ((reply = conf_dns_lookup(aconf)))
262 memcpy(&aconf->ipnum, reply->hp->h_addr, sizeof(struct in_addr));
266 * conf_find_server - find a server by name or hostname
267 * returns a server conf item pointer if found, 0 otherwise
269 * NOTE: at some point when we don't have to scan the entire
270 * list it may be cheaper to look for server names and host
271 * names in separate loops (original code did it that way)
273 struct ConfItem* conf_find_server(const char* name)
275 struct ConfItem* conf;
278 for (conf = GlobalConfList; conf; conf = conf->next) {
279 if (CONF_SERVER == conf->status) {
281 * Check first servernames, then try hostnames.
282 * XXX - match returns 0 if there _is_ a match... guess they
283 * haven't decided what true is yet
285 if (0 == match(name, conf->name))
293 * conf_eval_crule - evaluate connection rules
294 * returns the name of the rule triggered if found, 0 otherwise
296 * Evaluate connection rules... If no rules found, allow the
297 * connect. Otherwise stop with the first true rule (ie: rules
298 * are ored together. Oper connects are effected only by D
299 * lines (CRULE_ALL) not d lines (CRULE_AUTO).
301 const char* conf_eval_crule(const char* name, int mask)
303 struct CRuleConf* p = cruleConfList;
306 for ( ; p; p = p->next) {
307 if (0 != (p->type & mask) && 0 == match(p->hostmask, name)) {
308 if (crule_eval(p->node))
316 * Remove all conf entries from the client except those which match
317 * the status field mask.
319 void det_confs_butmask(struct Client* cptr, int mask)
325 for (link = cptr->confs; link; link = next) {
327 if ((link->value.aconf->status & mask) == 0)
328 detach_conf(cptr, link->value.aconf);
333 * check_limit_and_attach - check client limits and attach I:line
335 * Made it accept 1 charactor, and 2 charactor limits (0->99 now),
336 * and dislallow more than 255 people here as well as in ipcheck.
337 * removed the old "ONE" scheme too.
338 * -- Isomer 2000-06-22
340 static enum AuthorizationCheckResult
341 check_limit_and_attach(struct Client* cptr, struct ConfItem* aconf)
346 if (IsDigit(*aconf->passwd) && !aconf->passwd[1])
347 number = *aconf->passwd-'0';
348 else if (IsDigit(*aconf->passwd) && IsDigit(aconf->passwd[1]) &&
350 number = (*aconf->passwd-'0')*10+(aconf->passwd[1]-'0');
352 if (IPcheck_nr(cptr) > number)
353 return ACR_TOO_MANY_FROM_IP;
354 return attach_conf(cptr, aconf);
358 * Find the first (best) I line to attach.
360 enum AuthorizationCheckResult attach_iline(struct Client* cptr)
362 struct ConfItem* aconf;
365 static char uhost[HOSTLEN + USERLEN + 3];
366 static char fullname[HOSTLEN + 1];
367 struct hostent* hp = 0;
372 hp = cptr->dns_reply->hp;
374 for (aconf = GlobalConfList; aconf; aconf = aconf->next) {
375 if (aconf->status != CONF_CLIENT)
377 if (aconf->port && aconf->port != cptr->listener->port)
379 if (!aconf->host || !aconf->name)
382 for (i = 0, hname = hp->h_name; hname; hname = hp->h_aliases[i++]) {
383 ircd_strncpy(fullname, hname, HOSTLEN);
384 fullname[HOSTLEN] = '\0';
386 Debug((DEBUG_DNS, "a_il: %s->%s", cptr->sockhost, fullname));
388 if (strchr(aconf->name, '@')) {
389 strcpy(uhost, cptr->username);
394 strncat(uhost, fullname, sizeof(uhost) - 1 - strlen(uhost));
395 uhost[sizeof(uhost) - 1] = 0;
396 if (0 == match(aconf->name, uhost)) {
397 if (strchr(uhost, '@'))
398 cptr->flags |= FLAGS_DOID;
399 return check_limit_and_attach(cptr, aconf);
403 if (strchr(aconf->host, '@')) {
404 ircd_strncpy(uhost, cptr->username, sizeof(uhost) - 2);
405 uhost[sizeof(uhost) - 2] = 0;
410 strncat(uhost, cptr->sock_ip, sizeof(uhost) - 1 - strlen(uhost));
411 uhost[sizeof(uhost) - 1] = 0;
412 if (match(aconf->host, uhost))
414 if (strchr(uhost, '@'))
415 cptr->flags |= FLAGS_DOID;
417 return check_limit_and_attach(cptr, aconf);
419 return ACR_NO_AUTHORIZATION;
422 static int is_attached(struct ConfItem *aconf, struct Client *cptr)
426 for (lp = cptr->confs; lp; lp = lp->next) {
427 if (lp->value.aconf == aconf)
436 * Associate a specific configuration entry to a *local*
437 * client (this is the one which used in accepting the
438 * connection). Note, that this automaticly changes the
439 * attachment if there was an old one...
441 enum AuthorizationCheckResult attach_conf(struct Client *cptr, struct ConfItem *aconf)
445 if (is_attached(aconf, cptr))
446 return ACR_ALREADY_AUTHORIZED;
447 if (IsIllegal(aconf))
448 return ACR_NO_AUTHORIZATION;
449 if ((aconf->status & (CONF_LOCOP | CONF_OPERATOR | CONF_CLIENT)) &&
450 ConfLinks(aconf) >= ConfMaxLinks(aconf) && ConfMaxLinks(aconf) > 0)
451 return ACR_TOO_MANY_IN_CLASS; /* Use this for printing error message */
453 lp->next = cptr->confs;
454 lp->value.aconf = aconf;
457 if (aconf->status & CONF_CLIENT_MASK)
462 const struct LocalConf* conf_get_local(void)
468 * attach_confs_byname
470 * Attach a CONF line to a client if the name passed matches that for
471 * the conf file (for non-C/N lines) or is an exact match (C/N lines
472 * only). The difference in behaviour is to stop C:*::* and N:*::*.
474 struct ConfItem* attach_confs_byname(struct Client* cptr, const char* name,
477 struct ConfItem* tmp;
478 struct ConfItem* first = NULL;
482 if (HOSTLEN < strlen(name))
485 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
486 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
487 assert(0 != tmp->name);
488 if (0 == match(tmp->name, name) || 0 == ircd_strcmp(tmp->name, name)) {
489 if (ACR_OK == attach_conf(cptr, tmp) && !first)
498 * Added for new access check meLazy
500 struct ConfItem* attach_confs_byhost(struct Client* cptr, const char* host,
503 struct ConfItem* tmp;
504 struct ConfItem* first = 0;
507 if (HOSTLEN < strlen(host))
510 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
511 if (0 != (tmp->status & statmask) && !IsIllegal(tmp)) {
512 assert(0 != tmp->host);
513 if (0 == match(tmp->host, host) || 0 == ircd_strcmp(tmp->host, host)) {
514 if (ACR_OK == attach_conf(cptr, tmp) && !first)
523 * find a conf entry which matches the hostname and has the same name.
525 struct ConfItem* find_conf_exact(const char* name, const char* user,
526 const char* host, int statmask)
528 struct ConfItem *tmp;
529 char userhost[USERLEN + HOSTLEN + 3];
532 ircd_snprintf(0, userhost, sizeof(userhost), "%s@%s", user, host);
534 ircd_strncpy(userhost, host, sizeof(userhost) - 1);
536 for (tmp = GlobalConfList; tmp; tmp = tmp->next) {
537 if (!(tmp->status & statmask) || !tmp->name || !tmp->host ||
538 0 != ircd_strcmp(tmp->name, name))
541 * Accept if the *real* hostname (usually sockecthost)
542 * socket host) matches *either* host or name field
543 * of the configuration.
545 if (match(tmp->host, userhost))
547 if (tmp->status & (CONF_OPERATOR | CONF_LOCOP)) {
548 if (tmp->clients < MaxLinks(tmp->conn_class))
559 struct ConfItem* find_conf_byname(struct SLink* lp, const char* name,
562 struct ConfItem* tmp;
565 if (HOSTLEN < strlen(name))
568 for (; lp; lp = lp->next) {
569 tmp = lp->value.aconf;
570 if (0 != (tmp->status & statmask)) {
571 assert(0 != tmp->name);
572 if (0 == ircd_strcmp(tmp->name, name) || 0 == match(tmp->name, name))
580 * Added for new access check meLazy
582 struct ConfItem* find_conf_byhost(struct SLink* lp, const char* host,
585 struct ConfItem* tmp = NULL;
588 if (HOSTLEN < strlen(host))
591 for (; lp; lp = lp->next) {
592 tmp = lp->value.aconf;
593 if (0 != (tmp->status & statmask)) {
594 assert(0 != tmp->host);
595 if (0 == match(tmp->host, host))
605 * Find a conf line using the IP# stored in it to search upon.
606 * Added 1/8/92 by Avalon.
608 struct ConfItem* find_conf_byip(struct SLink* lp, const char* ip,
611 struct ConfItem* tmp;
613 for (; lp; lp = lp->next) {
614 tmp = lp->value.aconf;
615 if (0 != (tmp->status & statmask)) {
616 if (0 == memcmp(&tmp->ipnum, ip, sizeof(struct in_addr)))
626 * - looks for a match on all given fields.
628 static struct ConfItem *find_conf_entry(struct ConfItem *aconf,
631 struct ConfItem *bconf;
634 mask &= ~CONF_ILLEGAL;
636 for (bconf = GlobalConfList; bconf; bconf = bconf->next) {
637 if (!(bconf->status & mask) || (bconf->port != aconf->port))
640 if ((EmptyString(bconf->host) && !EmptyString(aconf->host)) ||
641 (EmptyString(aconf->host) && !EmptyString(bconf->host)))
643 if (!EmptyString(bconf->host) && 0 != ircd_strcmp(bconf->host, aconf->host))
646 if ((EmptyString(bconf->passwd) && !EmptyString(aconf->passwd)) ||
647 (EmptyString(aconf->passwd) && !EmptyString(bconf->passwd)))
649 if (!EmptyString(bconf->passwd) && (!IsDigit(*bconf->passwd) || bconf->passwd[1])
651 && 0 != ircd_strcmp(bconf->passwd, "ONE")
653 && 0 != ircd_strcmp(bconf->passwd, aconf->passwd))
656 if ((EmptyString(bconf->name) && !EmptyString(aconf->name)) ||
657 (EmptyString(aconf->name) && !EmptyString(bconf->name)))
659 if (!EmptyString(bconf->name) && 0 != ircd_strcmp(bconf->name, aconf->name))
668 * If conf line is a class definition, create a class entry
669 * for it and make the conf_line illegal and delete it.
671 void conf_add_class(const char* const* fields, int count)
675 add_class(atoi(fields[1]), atoi(fields[2]), atoi(fields[3]),
676 atoi(fields[4]), atoi(fields[5]));
679 void conf_add_listener(const char* const* fields, int count)
687 if (count < 5 || EmptyString(fields[4]))
690 if (!EmptyString(fields[3])) {
691 const char* x = fields[3];
692 if ('S' == ToUpper(*x))
695 if ('H' == ToUpper(*x))
698 /* port vhost mask */
699 add_listener(atoi(fields[4]), fields[2], fields[1], is_server, is_hidden);
702 void conf_add_local(const char* const* fields, int count)
704 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[5])) {
705 ircd_log(L_CRIT, "Your M: line must have 6 fields!\n");
709 * these two can only be set the first time
711 if (0 == localConf.name) {
712 if (string_is_hostname(fields[1]))
713 DupString(localConf.name, fields[1]);
715 if (0 == localConf.numeric) {
716 localConf.numeric = atoi(fields[5]);
717 if (0 == localConf.numeric)
718 ircd_log(L_WARNING, "Your M: line must have a Numeric value greater than 0\n");
721 * these two can be changed while the server is running
723 if (string_is_address(fields[2])) {
724 if (INADDR_NONE == (localConf.vhost_address.s_addr = inet_addr(fields[2])))
725 localConf.vhost_address.s_addr = INADDR_ANY;
727 MyFree(localConf.description);
728 DupString(localConf.description, fields[3]);
730 * XXX - shouldn't be setting these directly here
732 ircd_strncpy(me.info, fields[3], REALLEN);
733 set_virtual_host(localConf.vhost_address);
736 void conf_add_admin(const char* const* fields, int count)
739 * if you have one, it MUST have 3 lines
742 ircd_log(L_CRIT, "Your A: line must have 4 fields!\n");
745 MyFree(localConf.location1);
746 DupString(localConf.location1, fields[1]);
748 MyFree(localConf.location2);
749 DupString(localConf.location2, fields[2]);
751 MyFree(localConf.contact);
752 DupString(localConf.contact, fields[3]);
756 * conf_add_crule - Create expression tree from connect rule and add it
759 void conf_add_crule(const char* const* fields, int count, int type)
761 struct CRuleNode* node;
764 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
767 if ((node = crule_parse(fields[3]))) {
768 struct CRuleConf* p = (struct CRuleConf*) MyMalloc(sizeof(struct CRuleConf));
771 DupString(p->hostmask, fields[1]);
772 collapse(p->hostmask);
774 DupString(p->rule, fields[3]);
778 p->next = cruleConfList;
783 void conf_erase_crule_list(void)
785 struct CRuleConf* next;
786 struct CRuleConf* p = cruleConfList;
788 for ( ; p; p = next) {
790 crule_free(&p->node);
798 const struct CRuleConf* conf_get_crule_list(void)
800 return cruleConfList;
803 void conf_add_server(const char* const* fields, int count)
805 struct ServerConf* server;
809 * missing host, password, or alias?
811 if (count < 6 || EmptyString(fields[1]) || EmptyString(fields[2]) || EmptyString(fields[3]))
816 if (string_is_hostname(fields[1]))
817 addr.s_addr = INADDR_NONE;
818 else if (INADDR_NONE == (addr.s_addr = inet_addr(fields[1])))
821 server = (struct ServerConf*) MyMalloc(sizeof(struct ServerConf));
823 DupString(server->hostname, fields[1]);
824 DupString(server->passwd, fields[2]);
825 DupString(server->alias, fields[3]);
826 server->address.s_addr = addr.s_addr;
827 server->port = atoi(fields[4]);
828 server->dns_pending = 0;
829 server->connected = 0;
831 server->conn_class = find_class(atoi(fields[5]));
833 server->next = serverConfList;
834 serverConfList = server;
836 // if (INADDR_NONE == server->address.s_addr)
837 // lookup_confhost(server);
840 void conf_add_deny(const char* const* fields, int count, int ip_kill)
842 struct DenyConf* conf;
844 if (count < 4 || EmptyString(fields[1]) || EmptyString(fields[3]))
847 conf = (struct DenyConf*) MyMalloc(sizeof(struct DenyConf));
849 memset(conf, 0, sizeof(struct DenyConf));
851 DupString(conf->hostmask, fields[1]);
852 collapse(conf->hostmask);
854 if (!EmptyString(fields[2])) {
855 const char* p = fields[2];
860 DupString(conf->message, p);
862 DupString(conf->usermask, fields[3]);
863 collapse(conf->usermask);
867 * Here we use the same kludge as in listener.c to parse
868 * a.b.c.d, or a.b.c.*, or a.b.c.d/e.
874 c_class = sscanf(conf->hostmask, "%d.%d.%d.%d/%d",
875 &ad[0], &ad[1], &ad[2], &ad[3], &bits2);
877 conf->bits = c_class * 8;
882 sprintf_irc(ipname, "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
885 * This ensures endian correctness
887 conf->s_addr = inet_addr(ipname);
888 Debug((DEBUG_DEBUG, "IPkill: %s = %08x/%i (%08x)", ipname,
889 conf->s_addr, conf->bits, NETMASK(conf->bits)));
891 conf->next = denyConfList;
895 void conf_erase_deny_list(void)
897 struct DenyConf* next;
898 struct DenyConf* p = denyConfList;
899 for ( ; p; p = next) {
909 const struct DenyConf* conf_get_deny_list(void)
915 * read_configuration_file
917 * Read configuration file.
919 * returns 0, if file cannot be opened
923 #define MAXCONFLINKS 150
925 int read_configuration_file(void)
927 enum { MAX_FIELDS = 15 };
935 struct ConfItem *aconf = 0;
938 const char* field_vector[MAX_FIELDS + 1];
940 Debug((DEBUG_DEBUG, "read_configuration_file: ircd.conf = %s", configfile));
941 if (0 == (file = fbopen(configfile, "r"))) {
945 while (fbgets(line, sizeof(line) - 1, file)) {
946 if ('#' == *line || IsSpace(*line))
949 if ((src = strchr(line, '\n')))
952 if (':' != line[1]) {
953 Debug((DEBUG_ERROR, "Bad config line: %s", line));
954 sendto_op_mask(SNO_OLDSNO,"Bad Config line");
959 * do escapes, quoting, comments, and field breakup in place
960 * in one pass with a poor mans state machine
962 field_vector[0] = line;
966 for (src = line, dest = line; *src; ) {
1021 field_vector[field_count++] = dest;
1022 if (field_count > MAX_FIELDS)
1038 if (field_count < 2 || EmptyString(field_vector[0]))
1044 aconf = make_conf();
1046 switch (*field_vector[0]) {
1047 case 'A': /* Name, e-mail address of administrator */
1048 case 'a': /* of this server. CONF_ADMIN */
1049 conf_add_admin(field_vector, field_count);
1050 aconf->status = CONF_ILLEGAL;
1052 case 'C': /* Server where I should try to connect */
1053 case 'c': /* in case of lp failures */
1055 aconf->status = CONF_SERVER;
1058 case 'D': /* CONF_CRULEALL */
1059 conf_add_crule(field_vector, field_count, CRULE_ALL);
1060 aconf->status = CONF_ILLEGAL;
1062 /* Connect rule - autos only */
1063 case 'd': /* CONF_CRULEAUTO */
1064 conf_add_crule(field_vector, field_count, CRULE_AUTO);
1065 aconf->status = CONF_ILLEGAL;
1067 case 'H': /* Hub server line */
1069 aconf->status = CONF_HUB;
1071 case 'I': /* Just plain normal irc client trying */
1072 case 'i': /* to connect me */
1073 aconf->status = CONF_CLIENT;
1075 case 'K': /* Kill user line on irc.conf */
1076 conf_add_deny(field_vector, field_count, 0);
1077 aconf->status = CONF_ILLEGAL;
1079 case 'k': /* Kill user line based on IP in ircd.conf */
1080 conf_add_deny(field_vector, field_count, 1);
1081 aconf->status = CONF_ILLEGAL;
1083 /* Operator. Line should contain at least */
1084 /* password and host where connection is */
1085 case 'L': /* guaranteed leaf server */
1087 aconf->status = CONF_LEAF;
1089 /* Me. Host field is name used for this host */
1090 /* and port number is the number of the port */
1092 case 'm': /* CONF_ME */
1093 conf_add_local(field_vector, field_count);
1094 aconf->status = CONF_ILLEGAL;
1097 aconf->status = CONF_OPERATOR;
1099 /* Local Operator, (limited privs --SRB) */
1101 aconf->status = CONF_LOCOP;
1103 case 'P': /* listen port line */
1104 case 'p': /* CONF_LISTEN_PORT */
1105 conf_add_listener(field_vector, field_count);
1106 aconf->status = CONF_ILLEGAL;
1108 case 'T': /* print out different motd's */
1109 case 't': /* based on hostmask - CONF_TLINES */
1110 motd_add(field_vector[1], field_vector[2]);
1111 aconf->status = CONF_ILLEGAL;
1113 case 'U': /* Underworld server, allowed to hack modes */
1114 case 'u': /* *Every* server on the net must define the same !!! */
1115 aconf->status = CONF_UWORLD;
1118 case 'y': /* CONF_CLASS */
1119 conf_add_class(field_vector, field_count);
1120 aconf->status = CONF_ILLEGAL;
1123 Debug((DEBUG_ERROR, "Error in config file: %s", line));
1124 sendto_op_mask(SNO_OLDSNO,"Unknown prefix in config file: %c", *field_vector[0]);
1125 aconf->status = CONF_ILLEGAL;
1128 if (IsIllegal(aconf))
1131 if (!EmptyString(field_vector[1]))
1132 DupString(aconf->host, field_vector[1]);
1134 if (!EmptyString(field_vector[2]))
1135 DupString(aconf->passwd, field_vector[2]);
1137 if (field_count > 3 && !EmptyString(field_vector[3]))
1138 DupString(aconf->name, field_vector[3]);
1140 if (field_count > 4 && !EmptyString(field_vector[4]))
1141 aconf->port = atoi(field_vector[4]);
1143 if (field_count > 5 && !EmptyString(field_vector[5]))
1144 aconf->conn_class = find_class(atoi(field_vector[5]));
1147 * Associate each conf line with a class by using a pointer
1148 * to the correct class record. -avalon
1150 if (aconf->status & CONF_CLIENT_MASK) {
1151 if (aconf->conn_class == 0)
1152 aconf->conn_class = find_class(0);
1154 if (aconf->status & CONF_CLIENT) {
1155 struct ConfItem *bconf;
1157 if ((bconf = find_conf_entry(aconf, aconf->status))) {
1159 bconf->status &= ~CONF_ILLEGAL;
1160 if (aconf->status == CONF_CLIENT) {
1162 * copy the password field in case it changed
1164 MyFree(bconf->passwd);
1165 bconf->passwd = aconf->passwd;
1168 ConfLinks(bconf) -= bconf->clients;
1169 bconf->conn_class = aconf->conn_class;
1170 if (bconf->conn_class)
1171 ConfLinks(bconf) += bconf->clients;
1177 if (aconf->status & CONF_SERVER) {
1178 if (ccount > MAXCONFLINKS || !aconf->host || strchr(aconf->host, '*') ||
1179 strchr(aconf->host, '?') || !aconf->name)
1182 if (aconf->status & (CONF_LOCOP | CONF_OPERATOR)) {
1183 if (!strchr(aconf->host, '@')) {
1185 int len = 3; /* *@\0 = 3 */
1187 len += strlen(aconf->host);
1188 newhost = (char*) MyMalloc(len);
1189 assert(0 != newhost);
1190 ircd_snprintf(0, newhost, len, "*@%s", aconf->host);
1191 MyFree(aconf->host);
1192 aconf->host = newhost;
1195 if (aconf->status & CONF_SERVER) {
1196 if (EmptyString(aconf->passwd))
1198 lookup_confhost(aconf);
1201 * Juped nicks are listed in the 'password' field of U:lines,
1202 * the list is comma separated and might be empty and/or contain
1203 * empty elements... the only limit is that it MUST be shorter
1204 * than 512 chars, or they will be cutted out :)
1206 if ((aconf->status == CONF_UWORLD) && (aconf->passwd) && (*aconf->passwd))
1207 addNickJupes(aconf->passwd);
1209 collapse(aconf->host);
1210 collapse(aconf->name);
1211 Debug((DEBUG_NOTICE,
1212 "Read Init: (%d) (%s) (%s) (%s) (%u) (%p)",
1213 aconf->status, aconf->host, aconf->passwd,
1214 aconf->name, aconf->port, aconf->conn_class));
1215 aconf->next = GlobalConfList;
1216 GlobalConfList = aconf;
1222 nextping = nextconnect = CurrentTime;
1229 * Actual REHASH service routine. Called with sig == 0 if it has been called
1230 * as a result of an operator issuing this command, else assume it has been
1231 * called as a result of the server receiving a HUP signal.
1233 int rehash(struct Client *cptr, int sig)
1235 struct ConfItem** tmp = &GlobalConfList;
1236 struct ConfItem* tmp2;
1237 struct Client* acptr;
1243 sendto_opmask_butone(0, SNO_OLDSNO,
1244 "Got signal SIGHUP, reloading ircd conf. file");
1246 while ((tmp2 = *tmp)) {
1247 if (tmp2->clients) {
1249 * Configuration entry is still in use by some
1250 * local clients, cannot delete it--mark it so
1251 * that it will be deleted when the last client
1254 if (CONF_CLIENT == (tmp2->status & CONF_CLIENT))
1260 tmp2->status |= CONF_ILLEGAL;
1267 conf_erase_crule_list();
1268 conf_erase_deny_list();
1272 * delete the juped nicks list
1277 flush_resolver_cache();
1279 class_mark_delete();
1280 mark_listeners_closing();
1282 read_configuration_file();
1285 class_delete_marked(); /* unless it fails */
1288 * Flush out deleted I and P lines although still in use.
1290 for (tmp = &GlobalConfList; (tmp2 = *tmp);) {
1291 if (CONF_ILLEGAL == (tmp2->status & CONF_ILLEGAL)) {
1300 for (i = 0; i <= HighestFd; i++) {
1301 if ((acptr = LocalClientArray[i])) {
1302 assert(!IsMe(acptr));
1303 if (IsServer(acptr)) {
1304 det_confs_butmask(acptr,
1305 ~(CONF_HUB | CONF_LEAF | CONF_UWORLD | CONF_ILLEGAL));
1306 attach_confs_byname(acptr, acptr->name,
1307 CONF_HUB | CONF_LEAF | CONF_UWORLD);
1309 /* Because admin's are getting so uppity about people managing to
1310 * get past K/G's etc, we'll "fix" the bug by actually explaining
1313 if ((found_g = find_kill(acptr))) {
1314 sendto_opmask_butone(0, found_g == -2 ? SNO_GLINE : SNO_OPERKILL,
1315 found_g == -2 ? "G-line active for %s%s" :
1316 "K-line active for %s%s",
1317 IsUnknown(acptr) ? "Unregistered Client ":"",
1318 get_client_name(acptr, HIDE_IP));
1319 if (exit_client(cptr, acptr, &me, found_g == -2 ? "G-lined" :
1320 "K-lined") == CPTR_KILLED)
1331 * Read configuration file.
1333 * returns 0, if file cannot be opened
1339 if (read_configuration_file()) {
1341 * make sure we're sane to start if the config
1342 * file read didn't get everything we need.
1343 * XXX - should any of these abort the server?
1344 * TODO: add warning messages
1346 if (0 == localConf.name || 0 == localConf.numeric)
1349 if (0 == localConf.location1)
1350 DupString(localConf.location1, "");
1351 if (0 == localConf.location2)
1352 DupString(localConf.location2, "");
1353 if (0 == localConf.contact)
1354 DupString(localConf.contact, "");
1366 * 0: Client may continue to try and connect
1367 * -1: Client was K/k:'d - sideeffect: reason was sent.
1368 * -2: Client was G/g:'d - sideeffect: reason was sent.
1370 * Client may have been sent a reason why they are denied, as above.
1372 int find_kill(struct Client *cptr)
1376 struct DenyConf* deny;
1377 struct Gline* agline = NULL;
1384 host = cptr->sockhost;
1385 name = cptr->user->username;
1387 assert(strlen(host) <= HOSTLEN);
1388 assert((name ? strlen(name) : 0) <= HOSTLEN);
1390 /* 2000-07-14: Rewrote this loop for massive speed increases.
1393 for (deny = denyConfList; deny; deny = deny->next) {
1394 if (0 != match(deny->usermask, name))
1397 if (EmptyString(deny->hostmask))
1400 if (deny->ip_kill) { /* k: by IP */
1401 Debug((DEBUG_DEBUG, "ip: %08x network: %08x/%i mask: %08x",
1402 cptr->ip.s_addr, deny->s_addr, deny->bits, NETMASK(deny->bits)));
1403 if ((cptr->ip.s_addr & NETMASK(deny->bits)) == deny->s_addr)
1406 else if (0 == match(deny->hostmask, host))
1410 if (EmptyString(deny->message))
1411 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP,
1412 ":Connection from your host is refused on this server.");
1415 killcomment(cptr, deny->message);
1417 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", deny->message);
1420 else if ((agline = gline_lookup(cptr, 0)) && GlineIsActive(agline)) {
1422 * find active glines
1423 * added a check against the user's IP address to find_gline() -Kev
1425 send_reply(cptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s.", GlineReason(agline));
1428 agline = 0; /* if a gline was found, it was inactive */
1439 * Ordinary client access check. Look for conf lines which have the same
1440 * status as the flags passed.
1442 enum AuthorizationCheckResult conf_check_client(struct Client *cptr)
1444 enum AuthorizationCheckResult acr = ACR_OK;
1448 if ((acr = attach_iline(cptr))) {
1449 Debug((DEBUG_DNS, "ch_cl: access denied: %s[%s]",
1450 cptr->name, cptr->sockhost));
1459 * Check access for a server given its name (passed in cptr struct).
1460 * Must check for all C/N lines which have a name which matches the
1461 * name given and a host which matches. A host alias which is the
1462 * same as the server name is also acceptable in the host field of a
1467 * -1 = Access denied
1470 int conf_check_server(struct Client *cptr)
1472 struct ConfItem* c_conf = NULL;
1475 Debug((DEBUG_DNS, "sv_cl: check access for %s[%s]",
1476 cptr->name, cptr->sockhost));
1478 if (IsUnknown(cptr) && !attach_confs_byname(cptr, cptr->name, CONF_SERVER)) {
1479 Debug((DEBUG_DNS, "No C/N lines for %s", cptr->sockhost));
1484 * We initiated this connection so the client should have a C and N
1485 * line already attached after passing through the connect_server()
1488 if (IsConnecting(cptr) || IsHandshake(cptr)) {
1489 c_conf = find_conf_byname(lp, cptr->name, CONF_SERVER);
1491 sendto_opmask_butone(0, SNO_OLDSNO, "Connect Error: lost C:line for %s",
1493 det_confs_butmask(cptr, 0);
1501 if (cptr->dns_reply) {
1503 struct hostent* hp = cptr->dns_reply->hp;
1504 const char* name = hp->h_name;
1506 * If we are missing a C or N line from above, search for
1507 * it under all known hostnames we have for this ip#.
1509 for (i = 0; name; name = hp->h_aliases[i++]) {
1510 if ((c_conf = find_conf_byhost(lp, name, CONF_SERVER))) {
1511 ircd_strncpy(cptr->sockhost, name, HOSTLEN);
1516 for (i = 0; hp->h_addr_list[i]; i++) {
1517 if ((c_conf = find_conf_byip(lp, hp->h_addr_list[i], CONF_SERVER)))
1524 * Check for C lines with the hostname portion the ip number
1525 * of the host the server runs on. This also checks the case where
1526 * there is a server connecting from 'localhost'.
1528 c_conf = find_conf_byhost(lp, cptr->sockhost, CONF_SERVER);
1532 * Attach by IP# only if all other checks have failed.
1533 * It is quite possible to get here with the strange things that can
1534 * happen when using DNS in the way the irc server does. -avalon
1537 c_conf = find_conf_byip(lp, (const char*) &cptr->ip, CONF_SERVER);
1539 * detach all conf lines that got attached by attach_confs()
1541 det_confs_butmask(cptr, 0);
1543 * if no C or no N lines, then deny access
1546 Debug((DEBUG_DNS, "sv_cl: access denied: %s[%s@%s]",
1547 cptr->name, cptr->username, cptr->sockhost));
1550 ircd_strncpy(cptr->name, c_conf->name, HOSTLEN);
1552 * attach the C and N lines to the client structure for later use.
1554 attach_conf(cptr, c_conf);
1555 attach_confs_byname(cptr, cptr->name, CONF_HUB | CONF_LEAF | CONF_UWORLD);
1557 if (INADDR_NONE == c_conf->ipnum.s_addr)
1558 c_conf->ipnum.s_addr = cptr->ip.s_addr;
1560 Debug((DEBUG_DNS, "sv_cl: access ok: %s[%s]", cptr->name, cptr->sockhost));