1 /* convert-conf.c - Convert ircu2.10.11 ircd.conf to ircu2.10.12 format.
2 * Copyright 2005 Michael Poole
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 #include <ctype.h> /* tolower() */
21 #include <stdio.h> /* *printf(), fgets() */
22 #include <stdlib.h> /* free(), strtol() */
23 #include <string.h> /* strlen(), memcpy(), strchr(), strspn() */
27 const char *admin_names[] = { "location", "contact", "contact", 0 },
28 *connect_names[] = { "host", "password", "name", "#port", "class", 0 },
29 *crule_names[] = { "server", "", "rule", 0 },
30 *general_names[] = { "name", "vhost", "description", "", "#numeric", 0 },
31 *motd_names[] = { "host", "file", 0 },
32 *class_names[] = { "name", "#pingfreq", "#connectfreq", "#maxlinks", "#sendq", 0 },
33 *removed_features[] = { "VIRTUAL_HOST", "TIMESEC", "OPERS_SEE_IN_SECRET_CHANNELS", "LOCOP_SEE_IN_SECRET_CHANNELS", "HIS_STATS_h", "HIS_DESYNCS", "AUTOHIDE", 0 };
34 char orig_line[512], line[512], dbuf[512];
35 char *fields[MAX_FIELDS + 1];
39 /*** GENERIC SUPPORT CODE ***/
41 static int split_line(char *input, char **output)
43 size_t quoted = 0, jj;
44 char *dest = dbuf, ch;
48 while (*input != '\0' && *input != '#') switch (ch = *input++) {
54 if (nfields >= MAX_FIELDS)
56 output[nfields++] = dest;
60 switch (ch = *input++) {
61 case 'b': *dest++ = '\b'; break;
62 case 'f': *dest++ = '\f'; break;
63 case 'n': *dest++ = '\n'; break;
64 case 'r': *dest++ = '\r'; break;
65 case 't': *dest++ = '\t'; break;
66 case 'v': *dest++ = '\v'; break;
67 default: *dest++ = ch; break;
70 case '"': quoted = !quoted; break;
71 default: *dest++ = ch; break;
75 for (jj = nfields; jj < MAX_FIELDS; ++jj)
80 static void simple_line(const char *block, const char **names, const char *extra)
84 /* Print the current line and start the new block. */
85 fprintf(stdout, "# %s\n%s {\n", orig_line, block);
87 /* Iterate over fields in input line, formatting each. */
88 for (ii = 0; ii < nfields && names[ii]; ++ii) {
89 if (!fields[ii][0] || !names[ii][0])
91 else if (names[ii][0] == '#')
92 fprintf(stdout, "\t%s = %s;\n", names[ii] + 1, fields[ii]);
94 fprintf(stdout, "\t%s = \"%s\";\n", names[ii], fields[ii]);
97 /* Close the new block (including any fixed-form text). */
99 fprintf(stdout, "\t%s\n", extra);
100 fputs("};\n", stdout);
103 #define dupstring(TARGET, SOURCE) do { free(TARGET); if (SOURCE) { size_t len = strlen(SOURCE) + 1; (TARGET) = malloc(len); memcpy((TARGET), (SOURCE), len); } else (TARGET) = 0; } while(0)
105 /*** MANAGING LISTS OF STRINGS ***/
108 struct string_list *next;
114 /** Find or insert the element from \a list that contains \a value.
115 * If an element of \a list already contains \a value, return it.
116 * Otherwise, append a new element to \a list containing \a value and
118 * @param[in,out] list A list of strings.
119 * @param[in] value A string to search for.
120 * @return A string list element from \a list containing \a value.
122 static struct string_list *string_get(struct string_list **list, const char *value)
124 struct string_list *curr;
125 size_t len = strlen(value), ii;
127 while ((curr = *list)) {
128 for (ii = 0; tolower(curr->value[ii]) == tolower(value[ii]) && ii < len; ++ii) ;
129 if (curr->value[ii] == '\0' && value[ii] == '\0')
134 *list = calloc(1, sizeof(**list) + len);
135 memcpy((*list)->value, value, len);
139 /*** SERVER CONNECTION RELATED CODE ***/
148 struct connect *next;
149 struct string_list *origins;
153 static struct connect *get_connect(const char *name)
155 struct connect *conn;
158 /* Look for a pre-existing connection with the same name. */
160 for (conn = connects; conn; conn = conn->next)
162 for (ii = 0; tolower(name[ii]) == tolower(conn->name[ii]) && ii < nlen; ++ii) ;
163 if (conn->name[ii] == '\0' && name[ii] == '\0')
167 /* If none was found, create a new one. */
170 conn = calloc(1, sizeof(*conn) + nlen);
171 for (ii = 0; ii < nlen; ++ii)
172 conn->name[ii] = name[ii];
173 conn->next = connects;
177 /* Return the connection. */
181 static void do_connect(void)
183 struct connect *conn = get_connect(fields[2]);
184 dupstring(conn->host, fields[0]);
185 dupstring(conn->password, fields[1]);
186 dupstring(conn->port, fields[3]);
187 dupstring(conn->class, fields[4]);
188 string_get(&conn->origins, orig_line);
191 static void do_hub(void)
193 struct connect *conn = get_connect(fields[2]);
194 dupstring(conn->hub, fields[0]);
195 dupstring(conn->maximum, fields[3]);
196 string_get(&conn->origins, orig_line);
199 static void do_leaf(void)
201 struct connect *conn = get_connect(fields[2]);
204 string_get(&conn->origins, orig_line);
207 static void finish_connects(void)
209 struct connect *conn;
210 struct string_list *sl;
212 for (conn = connects; conn; conn = conn->next)
214 for (sl = conn->origins; sl; sl = sl->next)
215 fprintf(stdout, "# %s\n", sl->value);
216 if (conn->host == NULL
217 || conn->password == NULL
218 || conn->class == NULL)
220 fprintf(stderr, "H:line missing C:line for %s\n", conn->name);
225 "Connect {\n\tname =\"%s\";\n\thost = \"%s\";\n"
226 "\tpassword = \"%s\";\n\tclass = \"%s\";\n",
227 conn->name, conn->host, conn->password, conn->class);
228 if (conn->port && conn->port[0] != '\0')
229 fprintf(stdout, "\tport = %s;\n", conn->port);
232 "# Every Connect block should have a port number.\n"
233 "# To prevent autoconnects, set autoconnect = no.\n"
235 "\tautoconnect = no;\n");
236 if (conn->maximum && conn->maximum[0] != '\0')
237 fprintf(stdout, "\tmaxhops = %s;\n", conn->maximum);
238 if (conn->hub && conn->hub[0] != '\0')
239 fprintf(stdout, "\thub = \"%s\";\n", conn->hub);
240 fprintf(stdout, "};\n\n");
245 /*** FEATURE MANAGEMENT CODE ***/
248 struct string_list *values;
249 struct string_list *origins;
250 struct feature *next;
254 struct remapped_feature {
256 const char *privilege;
257 int flags; /* 2 = global, 1 = local */
258 struct feature *feature;
259 } remapped_features[] = {
260 /* Specially handled privileges: If you change the index of
261 * anything with NULL privilege, change the code in
262 * finish_operators() to match!
264 { "CRYPT_OPER_PASSWORD", NULL, 0, 0 }, /* default: true */
265 { "OPER_KILL", NULL, 2, 0 }, /* default: true */
266 { "LOCAL_KILL_ONLY", NULL, 2, 0 }, /* default: false */
267 /* remapped features that affect all opers */
268 { "OPER_NO_CHAN_LIMIT", "chan_limit", 3, 0 },
269 { "OPER_MODE_LCHAN", "mode_lchan", 3, 0 },
270 { "OPER_WALK_THROUGH_LMODES", "walk_lchan", 3, 0 },
271 { "NO_OPER_DEOP_LCHAN", "deop_lchan", 3, 0 },
272 { "SHOW_INVISIBLE_USERS", "show_invis", 3, 0 },
273 { "SHOW_ALL_INVISIBLE_USERS", "show_all_invis", 3, 0 },
274 { "UNLIMIT_OPER_QUERY", "unlimit_query", 3, 0 },
275 /* remapped features affecting only global opers */
276 { "OPER_REHASH", "rehash", 2, 0 },
277 { "OPER_RESTART", "restart", 2, 0 },
278 { "OPER_DIE", "die", 2, 0 },
279 { "OPER_GLINE", "gline", 2, 0 },
280 { "OPER_LGLINE", "local_gline", 2, 0 },
281 { "OPER_JUPE", "jupe", 2, 0 },
282 { "OPER_LJUPE", "local_jupe", 2, 0 },
283 { "OPER_OPMODE", "opmode", 2, 0 },
284 { "OPER_LOPMODE", "local_opmode", 2, 0 },
285 { "OPER_FORCE_OPMODE", "force_opmode", 2, 0 },
286 { "OPER_FORCE_LOPMODE", "force_local_opmode", 2, 0 },
287 { "OPER_BADCHAN", "badchan", 2, 0 },
288 { "OPER_LBADCHAN", "local_badchan", 2, 0 },
289 { "OPER_SET", "set", 2, 0 },
290 { "OPER_WIDE_GLINE", "wide_gline", 2, 0 },
291 /* remapped features affecting only local opers */
292 { "LOCOP_KILL", "kill", 1, 0 },
293 { "LOCOP_REHASH", "rehash", 1, 0 },
294 { "LOCOP_RESTART", "restart", 1, 0 },
295 { "LOCOP_DIE", "die", 1, 0 },
296 { "LOCOP_LGLINE", "local_gline", 1, 0 },
297 { "LOCOP_LJUPE", "local_jupe", 1, 0 },
298 { "LOCOP_LOPMODE", "local_opmode", 1, 0 },
299 { "LOCOP_FORCE_LOPMODE", "force_local_opmode", 1, 0 },
300 { "LOCOP_LBADCHAN", "local_badchan", 1, 0 },
301 { "LOCOP_WIDE_GLINE", "wide_gline", 1, 0 },
305 static void do_feature(void)
307 struct feature *feat;
310 ii = strlen(fields[0]);
311 feat = calloc(1, sizeof(*feat) + ii);
313 feat->name[ii] = fields[0][ii];
314 feat->next = features;
316 string_get(&feat->origins, orig_line);
317 for (ii = 1; fields[ii] && fields[ii][0]; ++ii)
318 string_get(&feat->values, fields[ii]);
321 static void finish_features(void)
323 struct remapped_feature *rmf;
324 struct string_list *sl;
325 struct feature *feat;
328 fputs("Features {\n", stdout);
329 fputs("\t\"OPLEVELS\" = \"FALSE\";\n", stdout);
330 fputs("\t\"ZANNELS\" = \"FALSE\";\n", stdout);
332 for (feat = features; feat; feat = feat->next) {
333 /* Display the original feature line we are talking about. */
334 for (sl = feat->origins; sl; sl = sl->next)
335 fprintf(stdout, "# %s\n", sl->value);
337 /* See if the feature was remapped to an oper privilege. */
338 for (rmf = remapped_features; rmf->name; rmf++)
339 if (0 == strcmp(feat->name, rmf->name))
343 fprintf(stdout, "# Above feature mapped to an oper privilege.\n");
347 /* Was it removed? */
348 for (ii = 0; removed_features[ii]; ++ii)
349 if (0 == strcmp(feat->name, removed_features[ii]))
351 if (removed_features[ii]) {
352 fprintf(stdout, "# Above feature no longer exists.\n");
356 /* If it had no value before, drop it now since the lexer does
357 * not accept empty strings and the grammar does not accept
358 * empty stringlists.*/
360 fprintf(stdout, "# Above feature had no value.\n");
364 /* Wasn't remapped, wasn't removed: print it out. */
365 fprintf(stdout, "\t\"%s\" =", feat->name);
366 for (sl = feat->values; sl; sl = sl->next)
367 fprintf(stdout, " \"%s\"", sl->value);
368 fprintf(stdout, ";\n");
370 fputs("};\n\n", stdout);
374 /*** OPERATOR BLOCKS ***/
383 struct operator *next;
386 static void do_operator(int is_local)
388 struct operator *oper;
390 oper = calloc(1, sizeof(*oper));
391 dupstring(oper->host, fields[0]);
392 dupstring(oper->password, fields[1]);
393 dupstring(oper->name, fields[2]);
394 dupstring(oper->class, fields[4]);
395 dupstring(oper->origin, orig_line);
396 oper->is_local = is_local;
397 oper->next = operators;
401 static void finish_operators(void)
403 struct remapped_feature *remap;
404 struct operator *oper;
405 struct feature *feat;
407 int global_kill = 0, mask = 0;
410 if ((feat = remapped_features[0].feature) && feat->values
411 && 0 == strcmp(feat->values->value, "FALSE"))
414 if ((feat = remapped_features[1].feature) && feat->values
415 && 0 == strcmp(feat->values->value, "FALSE"))
417 else if ((feat = remapped_features[2].feature) && feat->values
418 && 0 == strcmp(feat->values->value, "FALSE"))
421 for (oper = operators; oper; oper = oper->next) {
422 fprintf(stdout, "# %s\nOperator {\n\tname = \"%s\";\n"
423 "\thost = \"%s\";\n\tpassword = \"%s%s\";\n"
424 "\tclass = \"%s\";\n",
425 oper->origin, oper->name, oper->host, pw_salt,
426 oper->password, oper->class);
427 if (oper->is_local) {
428 fputs("\tlocal = yes;\n", stdout);
431 fputs("\tlocal = no;\n", stdout);
432 if (global_kill == 1)
433 fputs("\tkill = no;\n\tlocal_kill = no;\n", stdout);
434 else if (global_kill == 2)
435 fputs("\tkill = no;\n\tlocal_kill = yes;\n", stdout);
438 for (ii = 0; (remap = &remapped_features[ii++])->name; ) {
439 if (!remap->feature || !remap->privilege
440 || !remap->feature->values || !(remap->flags & mask))
442 fprintf(stdout, "\t%s = %s;\n", remap->privilege,
443 strcmp(remap->feature->values->value, "TRUE") ? "no" : "yes");
445 fputs("};\n\n", stdout);
449 /*** OTHER CONFIG TRANSFORMS ***/
451 static void do_kill(void)
453 const char *host = fields[0], *reason = fields[1], *user = fields[2];
455 if (!memcmp(host, "$R", 3)) {
456 fprintf(stderr, "Empty realname K: line at line %u.\n", lineno);
460 /* Print the current line and start the new block. */
461 fprintf(stdout, "# %s\nKill {\n", orig_line);
463 /* Translate the user-matching portions. */
464 if (host[0] == '$' && host[1] == 'R') {
465 /* Realname kill, possibly with a username */
466 fprintf(stdout, "\trealname = \"%s\";\n", host + 2);
467 if (user[0] != '\0' && (user[0] != '*' || user[1] != '\0'))
468 fprintf(stdout, "\thost = \"%s@*\";\n", user);
470 /* Normal host or IP-based kill */
471 if (user[0] != '\0' && (user[0] != '*' || user[1] != '\0'))
472 fprintf(stdout, "\thost = \"%s@%s\";\n", user, host);
474 fprintf(stdout, "\thost = \"%s\";\n", host);
477 /* Translate the reason section. */
478 if (reason[0] == '!')
479 fprintf(stdout, "\tfile = \"%s\";\n", reason + 1);
481 fprintf(stdout, "\treason = \"%s\";\n", reason);
483 /* Close the block. */
484 fprintf(stdout, "};\n");
487 static void do_port(void)
489 const char *ipmask = fields[0], *iface = fields[1], *flags = fields[2], *port = fields[3];
491 /* Print the current line and start the new block. */
492 fprintf(stdout, "# %s\nPort {\n", orig_line);
494 /* Print the easy fields. */
495 fprintf(stdout, "\tport = %s;\n", port);
496 if (iface && iface[0] != '\0')
497 fprintf(stdout, "\tvhost = \"%s\";\n", iface);
498 if (ipmask && ipmask[0] != '\0')
499 fprintf(stdout, "\tmask = \"%s\";\n", ipmask);
501 /* Translate flag field. */
502 while (*flags) switch (*flags++) {
503 case 'C': case 'c': /* client port is default state */; break;
504 case 'S': case 's': fprintf(stdout, "\tserver = yes;\n"); break;
505 case 'H': case 'h': fprintf(stdout, "\thidden = yes;\n"); break;
508 /* Close the block. */
509 fprintf(stdout, "};\n");
512 struct string_list *quarantines;
514 static void do_quarantine(void)
516 struct string_list *q;
517 q = string_get(&quarantines, fields[0]);
518 dupstring(q->origin, orig_line);
519 dupstring(q->extra, fields[1]);
522 static void finish_quarantines(void)
524 struct string_list *sl;
528 fputs("Quarantine {\n", stdout);
529 for (sl = quarantines; sl; sl = sl->next)
530 fprintf(stdout, "# %s\n\t\"%s\" = \"%s\";\n", sl->origin, sl->value, sl->extra);
531 fputs("};\n\n", stdout);
535 static void do_uworld(void)
537 fprintf(stdout, "# %s\n", orig_line);
538 if (fields[0] && fields[0][0])
539 fprintf(stdout, "Uworld { name = \"%s\"; };\n", fields[0]);
540 if (fields[1] && fields[1][0])
541 fprintf(stdout, "Jupe { nick = \"%s\"; };\n", fields[1]);
544 static void emit_client(const char *mask, const char *passwd, const char *class, long maxlinks, int is_ip)
549 delim = strchr(mask, '@');
553 len = strspn(delim, "0123456789.*");
555 fprintf(stderr, "Invalid IP mask on line %u.\n", lineno);
558 fprintf(stdout, "Client {\n\tusername = \"%s\";\n\tip = \"%s\";\n", mask, delim);
560 fprintf(stdout, "Client {\n\tusername =\"%s\";\n\thost = \"%s\";\n", mask, delim);
563 len = strspn(mask, "0123456789.*");
566 fprintf(stdout, "Client {\n\tip = \"%s\";\n", mask);
568 if (!strchr(mask, '.') && !strchr(mask, '*'))
570 fprintf(stdout, "Client {\n\thost = \"%s\";\n", mask);
574 fprintf(stdout, "\tpassword = \"%s\";\n", passwd);
577 fprintf(stdout, "\tmaxlinks = %ld;\n", maxlinks);
579 fprintf(stdout, "\tclass = \"%s\";\n};\n", class);
582 static void do_client(void)
584 char *passwd = NULL, *delim;
587 /* Print the current line. */
588 fprintf(stdout, "# %s\n", orig_line);
590 /* See if the password is really a maxlinks count. */
591 maxlinks = strtol(fields[1], &delim, 10);
592 if (fields[1][0] == '\0')
594 else if (maxlinks < 0 || maxlinks > 99 || *delim != '\0')
597 /* Translate the IP and host mask fields into blocks. */
598 emit_client(fields[0], passwd, fields[4], maxlinks, 1);
599 emit_client(fields[2], passwd, fields[4], maxlinks, 0);
602 int main(int argc, char *argv[])
608 else if (!(ifile = fopen(argv[1], "rt"))) {
609 fprintf(stderr, "Unable to open file %s for input.\n", argv[1]);
613 for (lineno = 1; fgets(line, sizeof(line), ifile); ++lineno) {
614 /* Read line and pass comments through. */
615 size_t len = strlen(line);
616 if (line[0] == '#') {
620 /* Strip trailing whitespace. */
621 while (len > 0 && isspace(line[len-1]))
623 /* Pass blank lines through. */
628 /* Skip but report invalid lines. */
629 if (line[1] != ':') {
630 fprintf(stdout, "# %s\n", line);
631 fprintf(stderr, "Invalid input line %d.\n", lineno);
634 /* Copy the original line into a reusable variable. */
635 strcpy(orig_line, line);
636 /* Split line into fields. */
637 nfields = split_line(line + 2, fields);
639 /* Process the input line. */
641 case 'A': case 'a': simple_line("Admin", admin_names, NULL); break;
642 case 'C': case 'c': do_connect(); break;
643 case 'D': simple_line("CRule", crule_names, "all = yes;"); break;
644 case 'd': simple_line("CRule", crule_names, NULL); break;
645 case 'F': case 'f': do_feature(); break;
646 case 'H': case 'h': do_hub(); break;
647 case 'I': case 'i': do_client(); break;
648 case 'K': case 'k': do_kill(); break;
649 case 'L': case 'l': do_leaf(); break;
650 case 'M': case 'm': simple_line("General", general_names, NULL); break;
651 case 'O': do_operator(0); break;
652 case 'o': do_operator(1); break;
653 case 'P': case 'p': do_port(); break;
654 case 'Q': case 'q': do_quarantine(); break;
655 case 'T': case 't': simple_line("Motd", motd_names, NULL); break;
656 case 'U': case 'u': do_uworld(); break;
657 case 'Y': case 'y': simple_line("Class", class_names, NULL); break;
659 fprintf(stderr, "Unknown line %u with leading character '%c'.\n", lineno, line[0]);
666 fputs("\n# The following lines were intentionally moved and rearranged."
667 "\n# Our apologies for any inconvenience this may cause."
670 finish_quarantines();