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_ADMIN 0x0100
46 #define CONF_CLASS 0x0400
47 #define CONF_LISTEN_PORT 0x2000
48 #define CONF_CRULEALL 0x00200000
49 #define CONF_CRULEAUTO 0x00400000
50 #define CONF_TLINES 0x00800000
53 #define CONF_CRULE (CONF_CRULEALL | CONF_CRULEAUTO)
57 * For the connect rule patch.. these really should be in a header,
58 * but i see h.h isn't included for some reason.. so they're here.
62 struct CRuleNode* crule_parse(const char* rule);
63 void crule_free(struct CRuleNode** elem);
65 static void new_class(int cn);
66 static char confchar(unsigned int status);
67 static char *getfield(char *newline);
68 static int validate(struct ConfItem *top);
69 static struct ConfItem *chk_initconf(void);
70 static struct ConfClass *get_class(int cn, int ism);
72 static int numclasses = 0, *classarr = (int *)NULL, debugflag = 0;
73 static char *chk_configfile = CPATH;
74 static char nullfield[] = "";
75 static char maxsendq[12];
77 /* A few dummy variables and functions needed to link with runmalloc.o */
80 void debug(int level, const char *form, ...)
86 vfprintf(stderr, form, vl);
87 fprintf(stderr, "\n");
91 void sendto_one(struct Client *to, char *pattern, ...)
94 char *rpl_str(int numeric)
99 int main(int argc, char *argv[])
101 const char *dpath = DPATH;
102 chk_configfile = "ircd.conf";
119 fprintf(stderr, "-d: Missing path\n");
125 if (isdigit(argv[1][2]))
126 debugflag = atoi(&argv[1][2]);
129 fprintf(stderr, "Ignoring unknown option -%c\n", argv[1][1]);
133 chk_configfile = argv[1];
140 fprintf(stderr, "chdir(\"%s\") : %s\n", dpath,
141 (strerror(errno)) ? strerror(errno) : "Unknown error");
144 else if (debugflag > 1)
145 fprintf(stderr, "chdir(\"%s\") : Success", dpath);
149 return validate(chk_initconf());
155 * Read configuration file.
157 * Returns -1, if file cannot be opened
160 static struct ConfItem *chk_initconf(void)
165 struct CRuleNode* crule;
166 int ccount = 0, flags = 0;
167 struct ConfItem *aconf = NULL, *ctop = NULL;
169 fprintf(stderr, "chk_initconf(): ircd.conf = %s\n", chk_configfile);
170 if (NULL == (file = fbopen(chk_configfile, "r")))
176 while (fbgets(line, sizeof(line) - 1, file))
182 MyFree(aconf->passwd);
187 aconf = (struct ConfItem*) MyMalloc(sizeof(struct ConfItem));
191 aconf->passwd = NULL;
193 aconf->confClass = NULL;
194 aconf->dns_pending = 0;
196 if ((tmp = strchr(line, '\n')))
199 * Do quoting of characters and # detection.
201 for (tmp = line; *tmp; ++tmp) {
203 switch (*(tmp + 1)) {
220 if ('\0' == *(tmp + 1))
223 strcpy(tmp + 1, tmp + 2);
225 else if (*tmp == '#')
228 if (!*line || *line == '#' || *line == '\n' ||
229 *line == ' ' || *line == '\t')
234 fprintf(stderr, "ERROR: Bad config line (%s)\n", line);
239 printf("\n%s\n", line);
242 tmp = getfield(line);
245 fprintf(stderr, "\tERROR: no fields found\n");
249 aconf->status = CONF_ILLEGAL;
253 case 'A': /* Name, e-mail address of administrator */
254 case 'a': /* of this server. */
255 aconf->status = CONF_ADMIN;
257 case 'C': /* Server where I should try to connect */
258 case 'c': /* in case of lp failures */
260 aconf->status = CONF_SERVER;
264 aconf->status = CONF_CRULEALL;
266 /* Connect rule - autos only */
268 aconf->status = CONF_CRULEAUTO;
270 case 'H': /* Hub server line */
272 aconf->status = CONF_HUB;
274 case 'I': /* Just plain normal irc client trying */
275 case 'i': /* to connect me */
276 aconf->status = CONF_CLIENT;
278 case 'K': /* Kill user line on irc.conf */
279 aconf->status = CONF_KILL;
281 case 'k': /* Kill user line based on IP in ircd.conf */
282 aconf->status = CONF_IPKILL;
284 /* Operator. Line should contain at least */
285 /* password and host where connection is */
286 case 'L': /* guaranteed leaf server */
288 aconf->status = CONF_LEAF;
290 /* Me. Host field is name used for this host */
291 /* and port number is the number of the port */
294 aconf->status = CONF_ME;
297 aconf->status = CONF_OPERATOR;
299 /* Local Operator, (limited privs --SRB) */
301 aconf->status = CONF_LOCOP;
303 case 'P': /* listen port line */
305 aconf->status = CONF_LISTEN_PORT;
309 aconf->status = CONF_TLINES;
313 aconf->status = CONF_UWORLD;
317 aconf->status = CONF_CLASS;
320 fprintf(stderr, "\tERROR: unknown conf line letter (%c)\n", *tmp);
324 if (IsIllegal(aconf))
327 for (;;) /* Fake loop, that I can use break here --msa */
329 if ((tmp = getfield(NULL)) == NULL)
331 DupString(aconf->host, tmp);
332 if ((tmp = getfield(NULL)) == NULL)
334 DupString(aconf->passwd, tmp);
335 if ((tmp = getfield(NULL)) == NULL)
337 DupString(aconf->name, tmp);
338 if ((tmp = getfield(NULL)) == NULL)
340 aconf->port = atoi(tmp);
341 if ((tmp = getfield(NULL)) == NULL)
343 if (!(aconf->status & (CONF_CLASS | CONF_ME)))
345 aconf->confClass = get_class(atoi(tmp), 0);
348 if (aconf->status & CONF_ME)
349 aconf->confClass = get_class(atoi(tmp), 1);
352 if (!aconf->confClass && (aconf->status & (CONF_SERVER |
353 CONF_ME | CONF_OPS | CONF_CLIENT)))
355 fprintf(stderr, "\tWARNING: No class. Default 0\n");
356 aconf->confClass = get_class(0, 0);
359 * If conf line is a class definition, create a class entry
360 * for it and make the conf_line illegal and delete it.
362 if (aconf->status & CONF_CLASS)
366 fprintf(stderr, "\tERROR: no class #\n");
371 fprintf(stderr, "\tWARNING: missing sendq field\n");
372 fprintf(stderr, "\t\t default: %d\n", DEFAULTMAXSENDQLENGTH);
373 sprintf(maxsendq, "%d", DEFAULTMAXSENDQLENGTH);
376 sprintf(maxsendq, "%d", atoi(tmp));
377 new_class(atoi(aconf->host));
378 aconf->confClass = get_class(atoi(aconf->host), 0);
382 if (aconf->status & CONF_LISTEN_PORT)
385 fprintf(stderr, "\tERROR: %s\n", "null host field in P-line");
386 else if (strchr(aconf->host, '/'))
387 fprintf(stderr, "\t%s\n", "WARNING: / present in P-line "
388 "for non-UNIXPORT configuration");
389 aconf->confClass = get_class(0, 0);
393 if (aconf->status & CONF_SERVER &&
394 (!aconf->host || strchr(aconf->host, '*') || strchr(aconf->host, '?')))
396 fprintf(stderr, "\tERROR: bad host field\n");
400 if (aconf->status & CONF_SERVER && BadPtr(aconf->passwd))
402 fprintf(stderr, "\tERROR: empty/no password field\n");
406 if (aconf->status & CONF_SERVER && !aconf->name)
408 fprintf(stderr, "\tERROR: bad name field\n");
412 if (aconf->status & (CONF_OPS))
413 if (!strchr(aconf->host, '@'))
416 int len = 3; /* *@\0 = 3 */
418 len += strlen(aconf->host);
419 newhost = (char *)MyMalloc(len);
420 sprintf(newhost, "*@%s", aconf->host);
422 aconf->host = newhost;
425 /* parse the connect rules to detect errors, but free
426 * any allocated storage immediately -- we're just looking
428 if (aconf->status & CONF_CRULE)
429 if ((crule = crule_parse(aconf->name)) != NULL)
432 if (!aconf->confClass)
433 aconf->confClass = get_class(0, 0);
434 sprintf(maxsendq, "%d", ConfClass(aconf));
436 if ((aconf->status & CONF_ADMIN) && (!aconf->name ||
437 !aconf->passwd || !aconf->host))
438 fprintf(stderr, "ERROR: Your A: line must have 4 fields!\n");
441 DupString(aconf->name, nullfield);
443 DupString(aconf->passwd, nullfield);
445 DupString(aconf->host, nullfield);
446 if (aconf->status & (CONF_ME | CONF_ADMIN))
448 if (flags & aconf->status)
449 fprintf(stderr, "ERROR: multiple %c-lines\n",
450 ToUpper(confchar(aconf->status)));
452 flags |= aconf->status;
456 printf("(%d) (%s) (%s) (%s) (%u) (%s)\n",
457 aconf->status, aconf->host, aconf->passwd,
458 aconf->name, aconf->port, maxsendq);
460 if (aconf->status & (CONF_SERVER | CONF_HUB | CONF_LEAF))
471 static struct ConfClass *get_class(int cn, int ism)
473 static struct ConfClass cls;
476 cls.conClass = (unsigned int)-1;
477 if ((cn >= 1) && (cn <= 64))
480 fprintf(stderr, "\tWARNING: server numeric %d is not 1-64\n", cn);
484 int i = numclasses - 1;
485 cls.conClass = (unsigned int)-1;
487 if (classarr[i] == cn)
493 fprintf(stderr, "\tWARNING: class %d not found\n", cn);
498 static void new_class(int cn)
502 classarr = (int *)MyRealloc(classarr, sizeof(int) * numclasses);
504 classarr = (int *)MyMalloc(sizeof(int));
505 classarr[numclasses - 1] = cn;
509 * field breakup for ircd.conf file.
511 static char *getfield(char *newline)
513 static char *line = NULL;
522 if ((end = strchr(line, ':')) == NULL)
525 if ((end = strchr(field, '\n')) == NULL)
526 end = field + strlen(field);
534 static int validate(struct ConfItem *top)
536 struct ConfItem *aconf, *bconf;
537 unsigned int otype, valid = 0;
542 for (aconf = top; aconf; aconf = aconf->next)
544 if (aconf->status & CONF_MATCH)
547 if (aconf->status & CONF_SERVER)
551 for (bconf = top; bconf; bconf = bconf->next)
553 if (bconf == aconf || !(bconf->status & otype))
555 if (bconf->confClass == aconf->confClass &&
556 0 == ircd_strcmp(bconf->name, aconf->name) &&
557 0 == ircd_strcmp(bconf->host, aconf->host))
559 aconf->status |= CONF_MATCH;
560 bconf->status |= CONF_MATCH;
566 for (bconf = top; bconf; bconf = bconf->next)
568 if ((bconf == aconf) || !(bconf->status & CONF_SERVER))
570 if (0 == ircd_strcmp(bconf->name, aconf->name))
572 aconf->status |= CONF_MATCH;
578 fprintf(stderr, "\n");
579 for (aconf = top; aconf; aconf = aconf->next) {
580 if (aconf->status & CONF_MATCH)
582 else if ('N' != confchar(aconf->status))
583 fprintf(stderr, "Unmatched %c:%s:%s:%s\n",
584 confchar(aconf->status), aconf->host, aconf->passwd, aconf->name);
586 return valid ? 0 : -1;
589 static char confchar(unsigned int status)
591 static char letrs[] = "ICNoOMKARYLPH";
594 status &= ~(CONF_MATCH | CONF_ILLEGAL);
596 for (; *s; s++, status >>= 1)