2 * IRC - Internet Relay Chat, ircd/chkconf.c
3 * Copyright (C) 1993 Darren Reed
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "ircd_alloc.h"
27 #include "ircd_chattr.h"
28 #include "ircd_string.h"
43 * stuff that isn't used by s_conf.c anymore
45 #define CONF_ME 0x0040
46 #define CONF_KILL 0x0080
47 #define CONF_ADMIN 0x0100
48 #define CONF_CLASS 0x0400
49 #define CONF_LISTEN_PORT 0x2000
50 #define CONF_IPKILL 0x00010000
51 #define CONF_CRULEALL 0x00200000
52 #define CONF_CRULEAUTO 0x00400000
53 #define CONF_TLINES 0x00800000
55 #define CONF_KLINE (CONF_KILL | CONF_IPKILL)
56 #define CONF_CRULE (CONF_CRULEALL | CONF_CRULEAUTO)
60 * For the connect rule patch.. these really should be in a header,
61 * but i see h.h isn't included for some reason.. so they're here.
65 struct CRuleNode* crule_parse(const char* rule);
66 void crule_free(struct CRuleNode** elem);
68 static void new_class(int cn);
69 static char confchar(unsigned int status);
70 static char *getfield(char *newline);
71 static int validate(struct ConfItem *top);
72 static struct ConfItem *chk_initconf(void);
73 static struct ConnectionClass *get_class(int cn, int ism);
75 static int numclasses = 0, *classarr = (int *)NULL, debugflag = 0;
76 static char *chk_configfile = CPATH;
77 static char nullfield[] = "";
78 static char maxsendq[12];
80 /* A few dummy variables and functions needed to link with runmalloc.o */
83 void debug(int level, const char *form, ...)
89 vfprintf(stderr, form, vl);
90 fprintf(stderr, "\n");
94 void sendto_one(struct Client *to, char *pattern, ...)
97 char *rpl_str(int numeric)
102 int main(int argc, char *argv[])
104 const char *dpath = DPATH;
105 chk_configfile = "ircd.conf";
122 fprintf(stderr, "-d: Missing path\n");
128 if (isdigit(argv[1][2]))
129 debugflag = atoi(&argv[1][2]);
132 fprintf(stderr, "Ignoring unknown option -%c\n", argv[1][1]);
136 chk_configfile = argv[1];
143 fprintf(stderr, "chdir(\"%s\") : %s\n", dpath,
144 (strerror(errno)) ? strerror(errno) : "Unknown error");
147 else if (debugflag > 1)
148 fprintf(stderr, "chdir(\"%s\") : Success", dpath);
152 return validate(chk_initconf());
158 * Read configuration file.
160 * Returns -1, if file cannot be opened
163 static struct ConfItem *chk_initconf(void)
168 struct CRuleNode* crule;
169 int ccount = 0, flags = 0;
170 struct ConfItem *aconf = NULL, *ctop = NULL;
172 fprintf(stderr, "chk_initconf(): ircd.conf = %s\n", chk_configfile);
173 if (NULL == (file = fbopen(chk_configfile, "r")))
179 while (fbgets(line, sizeof(line) - 1, file))
185 MyFree(aconf->passwd);
190 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
194 aconf->passwd = NULL;
196 aconf->conn_class = NULL;
197 aconf->dns_pending = 0;
199 if ((tmp = strchr(line, '\n')))
202 * Do quoting of characters and # detection.
204 for (tmp = line; *tmp; ++tmp) {
206 switch (*(tmp + 1)) {
223 if ('\0' == *(tmp + 1))
226 strcpy(tmp + 1, tmp + 2);
228 else if (*tmp == '#')
231 if (!*line || *line == '#' || *line == '\n' ||
232 *line == ' ' || *line == '\t')
237 fprintf(stderr, "ERROR: Bad config line (%s)\n", line);
242 printf("\n%s\n", line);
245 tmp = getfield(line);
248 fprintf(stderr, "\tERROR: no fields found\n");
252 aconf->status = CONF_ILLEGAL;
256 case 'A': /* Name, e-mail address of administrator */
257 case 'a': /* of this server. */
258 aconf->status = CONF_ADMIN;
260 case 'C': /* Server where I should try to connect */
261 case 'c': /* in case of lp failures */
263 aconf->status = CONF_SERVER;
267 aconf->status = CONF_CRULEALL;
269 /* Connect rule - autos only */
271 aconf->status = CONF_CRULEAUTO;
273 case 'H': /* Hub server line */
275 aconf->status = CONF_HUB;
277 case 'I': /* Just plain normal irc client trying */
278 case 'i': /* to connect me */
279 aconf->status = CONF_CLIENT;
281 case 'K': /* Kill user line on irc.conf */
282 aconf->status = CONF_KILL;
284 case 'k': /* Kill user line based on IP in ircd.conf */
285 aconf->status = CONF_IPKILL;
287 /* Operator. Line should contain at least */
288 /* password and host where connection is */
289 case 'L': /* guaranteed leaf server */
291 aconf->status = CONF_LEAF;
293 /* Me. Host field is name used for this host */
294 /* and port number is the number of the port */
297 aconf->status = CONF_ME;
300 aconf->status = CONF_OPERATOR;
302 /* Local Operator, (limited privs --SRB) */
304 aconf->status = CONF_LOCOP;
306 case 'P': /* listen port line */
308 aconf->status = CONF_LISTEN_PORT;
312 aconf->status = CONF_TLINES;
316 aconf->status = CONF_UWORLD;
320 aconf->status = CONF_CLASS;
323 fprintf(stderr, "\tERROR: unknown conf line letter (%c)\n", *tmp);
327 if (IsIllegal(aconf))
330 for (;;) /* Fake loop, that I can use break here --msa */
332 if ((tmp = getfield(NULL)) == NULL)
334 DupString(aconf->host, tmp);
335 if ((tmp = getfield(NULL)) == NULL)
337 DupString(aconf->passwd, tmp);
338 if ((tmp = getfield(NULL)) == NULL)
340 DupString(aconf->name, tmp);
341 if ((tmp = getfield(NULL)) == NULL)
343 aconf->port = atoi(tmp);
344 if ((tmp = getfield(NULL)) == NULL)
346 if (!(aconf->status & (CONF_CLASS | CONF_ME)))
348 aconf->conn_class = get_class(atoi(tmp), 0);
351 if (aconf->status & CONF_ME)
352 aconf->conn_class = get_class(atoi(tmp), 1);
355 if (!aconf->conn_class && (aconf->status & (CONF_SERVER |
356 CONF_ME | CONF_OPS | CONF_CLIENT)))
358 fprintf(stderr, "\tWARNING: No class. Default 0\n");
359 aconf->conn_class = get_class(0, 0);
362 * If conf line is a class definition, create a class entry
363 * for it and make the conf_line illegal and delete it.
365 if (aconf->status & CONF_CLASS)
369 fprintf(stderr, "\tERROR: no class #\n");
374 fprintf(stderr, "\tWARNING: missing sendq field\n");
375 fprintf(stderr, "\t\t default: %d\n", DEFAULTMAXSENDQLENGTH);
376 sprintf(maxsendq, "%d", DEFAULTMAXSENDQLENGTH);
379 sprintf(maxsendq, "%d", atoi(tmp));
380 new_class(atoi(aconf->host));
381 aconf->conn_class = get_class(atoi(aconf->host), 0);
385 if (aconf->status & CONF_LISTEN_PORT)
388 fprintf(stderr, "\tERROR: %s\n", "null host field in P-line");
389 else if (strchr(aconf->host, '/'))
390 fprintf(stderr, "\t%s\n", "WARNING: / present in P-line "
391 "for non-UNIXPORT configuration");
392 aconf->conn_class = get_class(0, 0);
396 if (aconf->status & CONF_SERVER &&
397 (!aconf->host || strchr(aconf->host, '*') || strchr(aconf->host, '?')))
399 fprintf(stderr, "\tERROR: bad host field\n");
403 if (aconf->status & CONF_SERVER && BadPtr(aconf->passwd))
405 fprintf(stderr, "\tERROR: empty/no password field\n");
409 if (aconf->status & CONF_SERVER && !aconf->name)
411 fprintf(stderr, "\tERROR: bad name field\n");
415 if (aconf->status & (CONF_OPS))
416 if (!strchr(aconf->host, '@'))
419 int len = 3; /* *@\0 = 3 */
421 len += strlen(aconf->host);
422 newhost = (char *)MyMalloc(len);
423 sprintf(newhost, "*@%s", aconf->host);
425 aconf->host = newhost;
428 /* parse the connect rules to detect errors, but free
429 * any allocated storage immediately -- we're just looking
431 if (aconf->status & CONF_CRULE)
432 if ((crule = crule_parse(aconf->name)) != NULL)
435 if (!aconf->conn_class)
436 aconf->conn_class = get_class(0, 0);
437 sprintf(maxsendq, "%d", ConfClass(aconf));
439 if ((aconf->status & CONF_ADMIN) && (!aconf->name ||
440 !aconf->passwd || !aconf->host))
441 fprintf(stderr, "ERROR: Your A: line must have 4 fields!\n");
444 DupString(aconf->name, nullfield);
446 DupString(aconf->passwd, nullfield);
448 DupString(aconf->host, nullfield);
449 if (aconf->status & (CONF_ME | CONF_ADMIN))
451 if (flags & aconf->status)
452 fprintf(stderr, "ERROR: multiple %c-lines\n",
453 ToUpper(confchar(aconf->status)));
455 flags |= aconf->status;
459 printf("(%d) (%s) (%s) (%s) (%u) (%s)\n",
460 aconf->status, aconf->host, aconf->passwd,
461 aconf->name, aconf->port, maxsendq);
463 if (aconf->status & (CONF_SERVER | CONF_HUB | CONF_LEAF))
474 static struct ConnectionClass *get_class(int cn, int ism)
476 static struct ConnectionClass cls;
479 cls.cc_class = (unsigned int)-1;
480 if ((cn >= 1) && (cn <= 64))
483 fprintf(stderr, "\tWARNING: server numeric %d is not 1-64\n", cn);
487 int i = numclasses - 1;
488 cls.cc_class = (unsigned int)-1;
490 if (classarr[i] == cn)
496 fprintf(stderr, "\tWARNING: class %d not found\n", cn);
501 static void new_class(int cn)
505 classarr = (int *)MyRealloc(classarr, sizeof(int) * numclasses);
507 classarr = (int *)MyMalloc(sizeof(int));
508 classarr[numclasses - 1] = cn;
512 * field breakup for ircd.conf file.
514 static char *getfield(char *newline)
516 static char *line = NULL;
525 if ((end = strchr(line, ':')) == NULL)
528 if ((end = strchr(field, '\n')) == NULL)
529 end = field + strlen(field);
537 static int validate(struct ConfItem *top)
539 struct ConfItem *aconf, *bconf;
540 unsigned int otype, valid = 0;
545 for (aconf = top; aconf; aconf = aconf->next)
547 if (aconf->status & CONF_MATCH)
550 if (aconf->status & CONF_SERVER)
554 for (bconf = top; bconf; bconf = bconf->next)
556 if (bconf == aconf || !(bconf->status & otype))
558 if (bconf->conn_class == aconf->conn_class &&
559 0 == ircd_strcmp(bconf->name, aconf->name) &&
560 0 == ircd_strcmp(bconf->host, aconf->host))
562 aconf->status |= CONF_MATCH;
563 bconf->status |= CONF_MATCH;
569 for (bconf = top; bconf; bconf = bconf->next)
571 if ((bconf == aconf) || !(bconf->status & CONF_SERVER))
573 if (0 == ircd_strcmp(bconf->name, aconf->name))
575 aconf->status |= CONF_MATCH;
581 fprintf(stderr, "\n");
582 for (aconf = top; aconf; aconf = aconf->next) {
583 if (aconf->status & CONF_MATCH)
585 else if ('N' != confchar(aconf->status))
586 fprintf(stderr, "Unmatched %c:%s:%s:%s\n",
587 confchar(aconf->status), aconf->host, aconf->passwd, aconf->name);
589 return valid ? 0 : -1;
592 static char confchar(unsigned int status)
594 static char letrs[] = "ICNoOMKARYLPH";
597 status &= ~(CONF_MATCH | CONF_ILLEGAL);
599 for (; *s; s++, status >>= 1)