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_ADMIN 0x0100
47 #define CONF_CLASS 0x0400
48 #define CONF_LISTEN_PORT 0x2000
49 #define CONF_CRULEALL 0x00200000
50 #define CONF_CRULEAUTO 0x00400000
51 #define CONF_TLINES 0x00800000
54 #define CONF_CRULE (CONF_CRULEALL | CONF_CRULEAUTO)
58 * For the connect rule patch.. these really should be in a header,
59 * but i see h.h isn't included for some reason.. so they're here.
63 struct CRuleNode* crule_parse(const char* rule);
64 void crule_free(struct CRuleNode** elem);
66 static void new_class(int cn);
67 static char confchar(unsigned int status);
68 static char *getfield(char *newline);
69 static int validate(struct ConfItem *top);
70 static struct ConfItem *chk_initconf(void);
71 static struct ConfClass *get_class(int cn, int ism);
73 static int numclasses = 0, *classarr = (int *)NULL, debugflag = 0;
74 static char *chk_configfile = CPATH;
75 static char nullfield[] = "";
76 static char maxsendq[12];
78 /* A few dummy variables and functions needed to link with runmalloc.o */
81 void debug(int level, const char *form, ...)
87 vfprintf(stderr, form, vl);
88 fprintf(stderr, "\n");
92 void sendto_one(struct Client *to, char *pattern, ...)
95 char *rpl_str(int numeric)
100 int main(int argc, char *argv[])
102 const char *dpath = DPATH;
103 chk_configfile = "ircd.conf";
120 fprintf(stderr, "-d: Missing path\n");
126 if (isdigit(argv[1][2]))
127 debugflag = atoi(&argv[1][2]);
130 fprintf(stderr, "Ignoring unknown option -%c\n", argv[1][1]);
134 chk_configfile = argv[1];
141 fprintf(stderr, "chdir(\"%s\") : %s\n", dpath,
142 (strerror(errno)) ? strerror(errno) : "Unknown error");
145 else if (debugflag > 1)
146 fprintf(stderr, "chdir(\"%s\") : Success", dpath);
150 return validate(chk_initconf());
156 * Read configuration file.
158 * Returns -1, if file cannot be opened
161 static struct ConfItem *chk_initconf(void)
166 struct CRuleNode* crule;
167 int ccount = 0, flags = 0;
168 struct ConfItem *aconf = NULL, *ctop = NULL;
170 fprintf(stderr, "chk_initconf(): ircd.conf = %s\n", chk_configfile);
171 if (NULL == (file = fbopen(chk_configfile, "r")))
177 while (fbgets(line, sizeof(line) - 1, file))
183 MyFree(aconf->passwd);
188 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
192 aconf->passwd = NULL;
194 aconf->confClass = NULL;
195 aconf->dns_pending = 0;
197 if ((tmp = strchr(line, '\n')))
200 * Do quoting of characters and # detection.
202 for (tmp = line; *tmp; ++tmp) {
204 switch (*(tmp + 1)) {
221 if ('\0' == *(tmp + 1))
224 strcpy(tmp + 1, tmp + 2);
226 else if (*tmp == '#')
229 if (!*line || *line == '#' || *line == '\n' ||
230 *line == ' ' || *line == '\t')
235 fprintf(stderr, "ERROR: Bad config line (%s)\n", line);
240 printf("\n%s\n", line);
243 tmp = getfield(line);
246 fprintf(stderr, "\tERROR: no fields found\n");
250 aconf->status = CONF_ILLEGAL;
254 case 'A': /* Name, e-mail address of administrator */
255 case 'a': /* of this server. */
256 aconf->status = CONF_ADMIN;
258 case 'C': /* Server where I should try to connect */
259 case 'c': /* in case of lp failures */
261 aconf->status = CONF_SERVER;
265 aconf->status = CONF_CRULEALL;
267 /* Connect rule - autos only */
269 aconf->status = CONF_CRULEAUTO;
271 case 'H': /* Hub server line */
273 aconf->status = CONF_HUB;
275 case 'I': /* Just plain normal irc client trying */
276 case 'i': /* to connect me */
277 aconf->status = CONF_CLIENT;
279 case 'K': /* Kill user line on irc.conf */
280 aconf->status = CONF_KILL;
282 case 'k': /* Kill user line based on IP in ircd.conf */
283 aconf->status = CONF_IPKILL;
285 /* Operator. Line should contain at least */
286 /* password and host where connection is */
287 case 'L': /* guaranteed leaf server */
289 aconf->status = CONF_LEAF;
291 /* Me. Host field is name used for this host */
292 /* and port number is the number of the port */
295 aconf->status = CONF_ME;
298 aconf->status = CONF_OPERATOR;
300 /* Local Operator, (limited privs --SRB) */
302 aconf->status = CONF_LOCOP;
304 case 'P': /* listen port line */
306 aconf->status = CONF_LISTEN_PORT;
310 aconf->status = CONF_TLINES;
314 aconf->status = CONF_UWORLD;
318 aconf->status = CONF_CLASS;
321 fprintf(stderr, "\tERROR: unknown conf line letter (%c)\n", *tmp);
325 if (IsIllegal(aconf))
328 for (;;) /* Fake loop, that I can use break here --msa */
330 if ((tmp = getfield(NULL)) == NULL)
332 DupString(aconf->host, tmp);
333 if ((tmp = getfield(NULL)) == NULL)
335 DupString(aconf->passwd, tmp);
336 if ((tmp = getfield(NULL)) == NULL)
338 DupString(aconf->name, tmp);
339 if ((tmp = getfield(NULL)) == NULL)
341 aconf->port = atoi(tmp);
342 if ((tmp = getfield(NULL)) == NULL)
344 if (!(aconf->status & (CONF_CLASS | CONF_ME)))
346 aconf->confClass = get_class(atoi(tmp), 0);
349 if (aconf->status & CONF_ME)
350 aconf->confClass = get_class(atoi(tmp), 1);
353 if (!aconf->confClass && (aconf->status & (CONF_SERVER |
354 CONF_ME | CONF_OPS | CONF_CLIENT)))
356 fprintf(stderr, "\tWARNING: No class. Default 0\n");
357 aconf->confClass = get_class(0, 0);
360 * If conf line is a class definition, create a class entry
361 * for it and make the conf_line illegal and delete it.
363 if (aconf->status & CONF_CLASS)
367 fprintf(stderr, "\tERROR: no class #\n");
372 fprintf(stderr, "\tWARNING: missing sendq field\n");
373 fprintf(stderr, "\t\t default: %d\n", DEFAULTMAXSENDQLENGTH);
374 sprintf(maxsendq, "%d", DEFAULTMAXSENDQLENGTH);
377 sprintf(maxsendq, "%d", atoi(tmp));
378 new_class(atoi(aconf->host));
379 aconf->confClass = get_class(atoi(aconf->host), 0);
383 if (aconf->status & CONF_LISTEN_PORT)
386 fprintf(stderr, "\tERROR: %s\n", "null host field in P-line");
387 else if (strchr(aconf->host, '/'))
388 fprintf(stderr, "\t%s\n", "WARNING: / present in P-line "
389 "for non-UNIXPORT configuration");
390 aconf->confClass = get_class(0, 0);
394 if (aconf->status & CONF_SERVER &&
395 (!aconf->host || strchr(aconf->host, '*') || strchr(aconf->host, '?')))
397 fprintf(stderr, "\tERROR: bad host field\n");
401 if (aconf->status & CONF_SERVER && BadPtr(aconf->passwd))
403 fprintf(stderr, "\tERROR: empty/no password field\n");
407 if (aconf->status & CONF_SERVER && !aconf->name)
409 fprintf(stderr, "\tERROR: bad name field\n");
413 if (aconf->status & (CONF_OPS))
414 if (!strchr(aconf->host, '@'))
417 int len = 3; /* *@\0 = 3 */
419 len += strlen(aconf->host);
420 newhost = (char *)MyMalloc(len);
421 sprintf(newhost, "*@%s", aconf->host);
423 aconf->host = newhost;
426 /* parse the connect rules to detect errors, but free
427 * any allocated storage immediately -- we're just looking
429 if (aconf->status & CONF_CRULE)
430 if ((crule = crule_parse(aconf->name)) != NULL)
433 if (!aconf->confClass)
434 aconf->confClass = get_class(0, 0);
435 sprintf(maxsendq, "%d", ConfClass(aconf));
437 if ((aconf->status & CONF_ADMIN) && (!aconf->name ||
438 !aconf->passwd || !aconf->host))
439 fprintf(stderr, "ERROR: Your A: line must have 4 fields!\n");
442 DupString(aconf->name, nullfield);
444 DupString(aconf->passwd, nullfield);
446 DupString(aconf->host, nullfield);
447 if (aconf->status & (CONF_ME | CONF_ADMIN))
449 if (flags & aconf->status)
450 fprintf(stderr, "ERROR: multiple %c-lines\n",
451 ToUpper(confchar(aconf->status)));
453 flags |= aconf->status;
457 printf("(%d) (%s) (%s) (%s) (%u) (%s)\n",
458 aconf->status, aconf->host, aconf->passwd,
459 aconf->name, aconf->port, maxsendq);
461 if (aconf->status & (CONF_SERVER | CONF_HUB | CONF_LEAF))
472 static struct ConfClass *get_class(int cn, int ism)
474 static struct ConfClass cls;
477 cls.conClass = (unsigned int)-1;
478 if ((cn >= 1) && (cn <= 64))
481 fprintf(stderr, "\tWARNING: server numeric %d is not 1-64\n", cn);
485 int i = numclasses - 1;
486 cls.conClass = (unsigned int)-1;
488 if (classarr[i] == cn)
494 fprintf(stderr, "\tWARNING: class %d not found\n", cn);
499 static void new_class(int cn)
503 classarr = (int *)MyRealloc(classarr, sizeof(int) * numclasses);
505 classarr = (int *)MyMalloc(sizeof(int));
506 classarr[numclasses - 1] = cn;
510 * field breakup for ircd.conf file.
512 static char *getfield(char *newline)
514 static char *line = NULL;
523 if ((end = strchr(line, ':')) == NULL)
526 if ((end = strchr(field, '\n')) == NULL)
527 end = field + strlen(field);
535 static int validate(struct ConfItem *top)
537 struct ConfItem *aconf, *bconf;
538 unsigned int otype, valid = 0;
543 for (aconf = top; aconf; aconf = aconf->next)
545 if (aconf->status & CONF_MATCH)
548 if (aconf->status & CONF_SERVER)
552 for (bconf = top; bconf; bconf = bconf->next)
554 if (bconf == aconf || !(bconf->status & otype))
556 if (bconf->confClass == aconf->confClass &&
557 0 == ircd_strcmp(bconf->name, aconf->name) &&
558 0 == ircd_strcmp(bconf->host, aconf->host))
560 aconf->status |= CONF_MATCH;
561 bconf->status |= CONF_MATCH;
567 for (bconf = top; bconf; bconf = bconf->next)
569 if ((bconf == aconf) || !(bconf->status & CONF_SERVER))
571 if (0 == ircd_strcmp(bconf->name, aconf->name))
573 aconf->status |= CONF_MATCH;
579 fprintf(stderr, "\n");
580 for (aconf = top; aconf; aconf = aconf->next) {
581 if (aconf->status & CONF_MATCH)
583 else if ('N' != confchar(aconf->status))
584 fprintf(stderr, "Unmatched %c:%s:%s:%s\n",
585 confchar(aconf->status), aconf->host, aconf->passwd, aconf->name);
587 return valid ? 0 : -1;
590 static char confchar(unsigned int status)
592 static char letrs[] = "ICNoOMKARYLPH";
595 status &= ~(CONF_MATCH | CONF_ILLEGAL);
597 for (; *s; s++, status >>= 1)