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)
58 /* DEFAULTMAXSENDQLENGTH went into the features subsystem... */
59 #define DEFAULTMAXSENDQLENGTH 40000
62 * For the connect rule patch.. these really should be in a header,
63 * but i see h.h isn't included for some reason.. so they're here.
67 struct CRuleNode* crule_parse(const char* rule);
68 void crule_free(struct CRuleNode** elem);
70 static void new_class(int cn);
71 static char confchar(unsigned int status);
72 static char *getfield(char *newline);
73 static int validate(struct ConfItem *top);
74 static struct ConfItem *chk_initconf(void);
75 static struct ConnectionClass *get_class(int cn, int ism);
77 static int numclasses = 0, *classarr = (int *)NULL, debugflag = 0;
78 static char *chk_configfile = "";
79 static char nullfield[] = "";
80 static char maxsendq[12];
82 /* A few dummy variables and functions needed to link with runmalloc.o */
85 void debug(int level, const char *form, ...)
91 vfprintf(stderr, form, vl);
92 fprintf(stderr, "\n");
96 void sendto_one(struct Client *to, char *pattern, ...)
99 char *rpl_str(int numeric)
104 int main(int argc, char *argv[])
106 const char *dpath = "./";
107 chk_configfile = "ircd.conf";
124 fprintf(stderr, "-d: Missing path\n");
130 if (isdigit(argv[1][2]))
131 debugflag = atoi(&argv[1][2]);
134 fprintf(stderr, "Ignoring unknown option -%c\n", argv[1][1]);
138 chk_configfile = argv[1];
145 fprintf(stderr, "chdir(\"%s\") : %s\n", dpath,
146 (strerror(errno)) ? strerror(errno) : "Unknown error");
149 else if (debugflag > 1)
150 fprintf(stderr, "chdir(\"%s\") : Success", dpath);
154 return validate(chk_initconf());
160 * Read configuration file.
162 * Returns -1, if file cannot be opened
165 static struct ConfItem *chk_initconf(void)
170 struct CRuleNode* crule;
171 int ccount = 0, flags = 0;
172 struct ConfItem *aconf = NULL, *ctop = NULL;
174 fprintf(stderr, "chk_initconf(): ircd.conf = %s\n", chk_configfile);
175 if (NULL == (file = fbopen(chk_configfile, "r")))
181 while (fbgets(line, sizeof(line) - 1, file))
187 MyFree(aconf->passwd);
192 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
196 aconf->passwd = NULL;
198 aconf->conn_class = NULL;
199 aconf->dns_pending = 0;
201 if ((tmp = strchr(line, '\n')))
204 * Do quoting of characters and # detection.
206 for (tmp = line; *tmp; ++tmp) {
208 switch (*(tmp + 1)) {
225 if ('\0' == *(tmp + 1))
228 strcpy(tmp + 1, tmp + 2);
230 else if (*tmp == '#')
233 if (!*line || *line == '#' || *line == '\n' ||
234 *line == ' ' || *line == '\t')
239 fprintf(stderr, "ERROR: Bad config line (%s)\n", line);
244 printf("\n%s\n", line);
247 tmp = getfield(line);
250 fprintf(stderr, "\tERROR: no fields found\n");
254 aconf->status = CONF_ILLEGAL;
258 case 'A': /* Name, e-mail address of administrator */
259 case 'a': /* of this server. */
260 aconf->status = CONF_ADMIN;
262 case 'C': /* Server where I should try to connect */
263 case 'c': /* in case of lp failures */
265 aconf->status = CONF_SERVER;
269 aconf->status = CONF_CRULEALL;
271 /* Connect rule - autos only */
273 aconf->status = CONF_CRULEAUTO;
275 case 'H': /* Hub server line */
277 aconf->status = CONF_HUB;
279 case 'I': /* Just plain normal irc client trying */
280 case 'i': /* to connect me */
281 aconf->status = CONF_CLIENT;
283 case 'K': /* Kill user line on irc.conf */
284 aconf->status = CONF_KILL;
286 case 'k': /* Kill user line based on IP in ircd.conf */
287 aconf->status = CONF_IPKILL;
289 /* Operator. Line should contain at least */
290 /* password and host where connection is */
291 case 'L': /* guaranteed leaf server */
293 aconf->status = CONF_LEAF;
295 /* Me. Host field is name used for this host */
296 /* and port number is the number of the port */
299 aconf->status = CONF_ME;
302 aconf->status = CONF_OPERATOR;
304 /* Local Operator, (limited privs --SRB) */
306 aconf->status = CONF_LOCOP;
308 case 'P': /* listen port line */
310 aconf->status = CONF_LISTEN_PORT;
314 aconf->status = CONF_TLINES;
318 aconf->status = CONF_UWORLD;
322 aconf->status = CONF_CLASS;
325 fprintf(stderr, "\tERROR: unknown conf line letter (%c)\n", *tmp);
329 if (IsIllegal(aconf))
332 for (;;) /* Fake loop, that I can use break here --msa */
334 if ((tmp = getfield(NULL)) == NULL)
336 DupString(aconf->host, tmp);
337 if ((tmp = getfield(NULL)) == NULL)
339 DupString(aconf->passwd, tmp);
340 if ((tmp = getfield(NULL)) == NULL)
342 DupString(aconf->name, tmp);
343 if ((tmp = getfield(NULL)) == NULL)
345 aconf->port = atoi(tmp);
346 if ((tmp = getfield(NULL)) == NULL)
348 if (!(aconf->status & (CONF_CLASS | CONF_ME)))
350 aconf->conn_class = get_class(atoi(tmp), 0);
353 if (aconf->status & CONF_ME)
354 aconf->conn_class = get_class(atoi(tmp), 1);
357 if (!aconf->conn_class && (aconf->status & (CONF_SERVER |
358 CONF_ME | CONF_OPS | CONF_CLIENT)))
360 fprintf(stderr, "\tWARNING: No class. Default 0\n");
361 aconf->conn_class = get_class(0, 0);
364 * If conf line is a class definition, create a class entry
365 * for it and make the conf_line illegal and delete it.
367 if (aconf->status & CONF_CLASS)
371 fprintf(stderr, "\tERROR: no class #\n");
376 fprintf(stderr, "\tWARNING: missing sendq field\n");
377 fprintf(stderr, "\t\t default: %d\n", DEFAULTMAXSENDQLENGTH);
378 sprintf(maxsendq, "%d", DEFAULTMAXSENDQLENGTH);
381 sprintf(maxsendq, "%d", atoi(tmp));
382 new_class(atoi(aconf->host));
383 aconf->conn_class = get_class(atoi(aconf->host), 0);
387 if (aconf->status & CONF_LISTEN_PORT)
390 fprintf(stderr, "\tERROR: %s\n", "null host field in P-line");
391 else if (strchr(aconf->host, '/'))
392 fprintf(stderr, "\t%s\n", "WARNING: / present in P-line "
393 "for non-UNIXPORT configuration");
394 aconf->conn_class = get_class(0, 0);
398 if (aconf->status & CONF_SERVER &&
399 (!aconf->host || strchr(aconf->host, '*') || strchr(aconf->host, '?')))
401 fprintf(stderr, "\tERROR: bad host field\n");
405 if (aconf->status & CONF_SERVER && BadPtr(aconf->passwd))
407 fprintf(stderr, "\tERROR: empty/no password field\n");
411 if (aconf->status & CONF_SERVER && !aconf->name)
413 fprintf(stderr, "\tERROR: bad name field\n");
417 if (aconf->status & (CONF_OPS))
418 if (!strchr(aconf->host, '@'))
421 int len = 3; /* *@\0 = 3 */
423 len += strlen(aconf->host);
424 newhost = (char *)MyMalloc(len);
425 sprintf(newhost, "*@%s", aconf->host);
427 aconf->host = newhost;
430 /* parse the connect rules to detect errors, but free
431 * any allocated storage immediately -- we're just looking
433 if (aconf->status & CONF_CRULE)
434 if ((crule = crule_parse(aconf->name)) != NULL)
437 if (!aconf->conn_class)
438 aconf->conn_class = get_class(0, 0);
439 sprintf(maxsendq, "%d", ConfClass(aconf));
441 if ((aconf->status & CONF_ADMIN) && (!aconf->name ||
442 !aconf->passwd || !aconf->host))
443 fprintf(stderr, "ERROR: Your A: line must have 4 fields!\n");
446 DupString(aconf->name, nullfield);
448 DupString(aconf->passwd, nullfield);
450 DupString(aconf->host, nullfield);
451 if (aconf->status & (CONF_ME | CONF_ADMIN))
453 if (flags & aconf->status)
454 fprintf(stderr, "ERROR: multiple %c-lines\n",
455 ToUpper(confchar(aconf->status)));
457 flags |= aconf->status;
461 printf("(%d) (%s) (%s) (%s) (%u) (%s)\n",
462 aconf->status, aconf->host, aconf->passwd,
463 aconf->name, aconf->port, maxsendq);
465 if (aconf->status & (CONF_SERVER | CONF_HUB | CONF_LEAF))
476 static struct ConnectionClass *get_class(int cn, int ism)
478 static struct ConnectionClass cls;
481 cls.cc_class = (unsigned int)-1;
482 if ((cn >= 1) && (cn <= 64))
485 fprintf(stderr, "\tWARNING: server numeric %d is not 1-64\n", cn);
489 int i = numclasses - 1;
490 cls.cc_class = (unsigned int)-1;
492 if (classarr[i] == cn)
498 fprintf(stderr, "\tWARNING: class %d not found\n", cn);
503 static void new_class(int cn)
507 classarr = (int *)MyRealloc(classarr, sizeof(int) * numclasses);
509 classarr = (int *)MyMalloc(sizeof(int));
510 classarr[numclasses - 1] = cn;
514 * field breakup for ircd.conf file.
516 static char *getfield(char *newline)
518 static char *line = NULL;
527 if ((end = strchr(line, ':')) == NULL)
530 if ((end = strchr(field, '\n')) == NULL)
531 end = field + strlen(field);
539 static int validate(struct ConfItem *top)
541 struct ConfItem *aconf, *bconf;
542 unsigned int otype, valid = 0;
547 for (aconf = top; aconf; aconf = aconf->next)
549 if (aconf->status & CONF_MATCH)
552 if (aconf->status & CONF_SERVER)
556 for (bconf = top; bconf; bconf = bconf->next)
558 if (bconf == aconf || !(bconf->status & otype))
560 if (bconf->conn_class == aconf->conn_class &&
561 0 == ircd_strcmp(bconf->name, aconf->name) &&
562 0 == ircd_strcmp(bconf->host, aconf->host))
564 aconf->status |= CONF_MATCH;
565 bconf->status |= CONF_MATCH;
571 for (bconf = top; bconf; bconf = bconf->next)
573 if ((bconf == aconf) || !(bconf->status & CONF_SERVER))
575 if (0 == ircd_strcmp(bconf->name, aconf->name))
577 aconf->status |= CONF_MATCH;
583 fprintf(stderr, "\n");
584 for (aconf = top; aconf; aconf = aconf->next) {
585 if (aconf->status & CONF_MATCH)
587 else if ('N' != confchar(aconf->status))
588 fprintf(stderr, "Unmatched %c:%s:%s:%s\n",
589 confchar(aconf->status), aconf->host, aconf->passwd, aconf->name);
591 return valid ? 0 : -1;
594 static char confchar(unsigned int status)
596 static char letrs[] = "ICNoOMKARYLPH";
599 status &= ~(CONF_MATCH | CONF_ILLEGAL);
601 for (; *s; s++, status >>= 1)