]>
Commit | Line | Data |
---|---|---|
189935b1 | 1 | /* convert-conf.c - Convert ircu2.10.11 ircd.conf to ircu2.10.12 format. |
2 | * Copyright 2005 Michael Poole | |
3 | * | |
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. | |
8 | * | |
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. | |
13 | * | |
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 | |
17 | * USA. | |
18 | */ | |
19 | ||
20 | #include <ctype.h> /* tolower(), toupper(), isdigit() */ | |
21 | #include <stdio.h> /* *printf(), fgets() */ | |
22 | #include <stdlib.h> /* free(), strtol() */ | |
23 | #include <string.h> /* strlen(), memcpy(), strchr(), strspn() */ | |
24 | ||
25 | #define MAX_FIELDS 5 | |
26 | ||
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", "OPERS_SEE_IN_SECRET_CHANNELS", "LOCOP_SEE_IN_SECRET_CHANNELS", 0 }; | |
34 | char orig_line[512], line[512], dbuf[512]; | |
35 | char *fields[MAX_FIELDS + 1]; | |
36 | unsigned int nfields; | |
37 | unsigned int lineno; | |
38 | ||
39 | /*** GENERIC SUPPORT CODE ***/ | |
40 | ||
41 | static int split_line(char *input, char **output) | |
42 | { | |
43 | size_t quoted = 0, jj; | |
44 | char *dest = dbuf, ch; | |
45 | ||
46 | nfields = 1; | |
47 | output[0] = dest; | |
48 | while (*input != '\0' && *input != '#') switch (ch = *input++) { | |
49 | case ':': | |
50 | if (quoted) | |
51 | *dest++ = ch; | |
52 | else { | |
53 | *dest++ = '\0'; | |
54 | if (nfields >= MAX_FIELDS) | |
55 | return nfields; | |
56 | output[nfields++] = dest; | |
57 | } | |
58 | break; | |
59 | case '\\': | |
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; | |
68 | } | |
69 | break; | |
70 | case '"': quoted = !quoted; break; | |
71 | default: *dest++ = ch; break; | |
72 | } | |
73 | ||
74 | *dest = '\0'; | |
75 | for (jj = nfields; jj < MAX_FIELDS; ++jj) | |
76 | output[jj] = dest; | |
77 | return nfields; | |
78 | } | |
79 | ||
80 | static void simple_line(const char *block, const char **names, const char *extra) | |
81 | { | |
82 | size_t ii; | |
83 | ||
84 | /* Print the current line and start the new block. */ | |
85 | fprintf(stdout, "# %s\n%s {\n", orig_line, block); | |
86 | ||
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]) | |
90 | continue; | |
91 | else if (names[ii][0] == '#') | |
92 | fprintf(stdout, "\t%s = %s;\n", names[ii] + 1, fields[ii]); | |
93 | else | |
94 | fprintf(stdout, "\t%s = \"%s\";\n", names[ii], fields[ii]); | |
95 | } | |
96 | ||
97 | /* Close the new block (including any fixed-form text). */ | |
98 | if (extra) | |
99 | fprintf(stdout, "\t%s\n", extra); | |
100 | fputs("};\n", stdout); | |
101 | } | |
102 | ||
103 | #define dupstring(TARGET, SOURCE) do { free(TARGET); if (SOURCE) { size_t len = strlen(SOURCE); (TARGET) = malloc(len+1); memcpy((TARGET), (SOURCE), len); } else (TARGET) = 0; } while(0) | |
104 | ||
105 | /*** MANAGING LISTS OF STRINGS ***/ | |
106 | ||
107 | struct string_list { | |
108 | struct string_list *next; | |
109 | char *origin; | |
110 | char *extra; | |
111 | char value[1]; | |
112 | }; | |
113 | ||
114 | static struct string_list *string_get(struct string_list **list, const char *value) | |
115 | { | |
116 | struct string_list *curr; | |
117 | size_t len = strlen(value), ii; | |
118 | ||
119 | while ((curr = *list)) { | |
120 | for (ii = 0; tolower(curr->value[ii]) == tolower(value[ii]) && ii < len; ++ii) ; | |
121 | if (curr->value[ii] == '\0' && value[ii] == '\0') | |
122 | return curr; | |
123 | list = &curr->next; | |
124 | } | |
125 | ||
126 | *list = calloc(1, sizeof(**list) + len); | |
127 | memcpy((*list)->value, value, len); | |
128 | return *list; | |
129 | } | |
130 | ||
131 | /*** SERVER CONNECTION RELATED CODE ***/ | |
132 | ||
133 | struct connect { | |
134 | char *host; | |
135 | char *password; | |
136 | char *port; | |
137 | char *class; | |
138 | char *hub; | |
139 | char *maximum; | |
140 | struct connect *next; | |
141 | struct string_list *origins; | |
142 | char name[1]; | |
143 | } *connects; | |
144 | ||
145 | static struct connect *get_connect(const char *name) | |
146 | { | |
147 | struct connect *conn; | |
148 | size_t ii, nlen; | |
149 | ||
150 | /* Look for a pre-existing connection with the same name. */ | |
151 | nlen = strlen(name); | |
152 | for (conn = connects; conn; conn = conn->next) | |
153 | { | |
154 | for (ii = 0; tolower(name[ii]) == conn->name[ii] && ii < nlen; ++ii) ; | |
155 | if (conn->name[ii] == '\0' && name[ii] == '\0') | |
156 | break; | |
157 | } | |
158 | ||
159 | /* If none was found, create a new one. */ | |
160 | if (!conn) | |
161 | { | |
162 | conn = calloc(1, sizeof(*conn) + nlen); | |
163 | for (ii = 0; ii < nlen; ++ii) | |
164 | conn->name[ii] = tolower(name[ii]); | |
165 | conn->next = connects; | |
166 | connects = conn; | |
167 | } | |
168 | ||
169 | /* Return the connection. */ | |
170 | return conn; | |
171 | } | |
172 | ||
173 | static void do_connect(void) | |
174 | { | |
175 | struct connect *conn = get_connect(fields[2]); | |
176 | dupstring(conn->host, fields[0]); | |
177 | dupstring(conn->password, fields[1]); | |
178 | dupstring(conn->port, fields[3]); | |
179 | dupstring(conn->class, fields[4]); | |
180 | string_get(&conn->origins, orig_line); | |
181 | } | |
182 | ||
183 | static void do_hub(void) | |
184 | { | |
185 | struct connect *conn = get_connect(fields[2]); | |
186 | dupstring(conn->hub, fields[0]); | |
187 | dupstring(conn->maximum, fields[3]); | |
188 | string_get(&conn->origins, orig_line); | |
189 | } | |
190 | ||
191 | static void do_leaf(void) | |
192 | { | |
193 | struct connect *conn = get_connect(fields[2]); | |
194 | free(conn->hub); | |
195 | conn->hub = 0; | |
196 | string_get(&conn->origins, orig_line); | |
197 | } | |
198 | ||
199 | static void finish_connects(void) | |
200 | { | |
201 | struct connect *conn; | |
202 | struct string_list *sl; | |
203 | ||
204 | for (conn = connects; conn; conn = conn->next) | |
205 | { | |
206 | for (sl = conn->origins; sl; sl = sl->next) | |
207 | fprintf(stdout, "# %s\n", sl->value); | |
208 | fprintf(stdout, | |
209 | "Connect {\n\tname =\"%s\";\n\thost = \"%s\";\n" | |
210 | "\tpassword = \"%s\";\n\tclass = \"%s\";\n", | |
211 | conn->name, conn->host, conn->password, conn->class); | |
212 | if (conn->port && conn->port[0] != '\0') | |
213 | fprintf(stdout, "\tport = %s;\n", conn->port); | |
214 | else | |
215 | fprintf(stdout, | |
216 | "# Every Connect block should have a port number.\n" | |
217 | "# To prevent autoconnects, set autoconnect = no.\n" | |
218 | "#\tport = 4400;\n" | |
219 | "\tautoconnect = no;\n"); | |
220 | if (conn->maximum && conn->maximum[0] != '\0') | |
221 | fprintf(stdout, "\tmaxhops = %s;\n", conn->maximum); | |
222 | if (conn->hub && conn->hub[0] != '\0') | |
223 | fprintf(stdout, "\thub = \"%s\";\n", conn->hub); | |
224 | fprintf(stdout, "};\n\n"); | |
225 | ||
226 | } | |
227 | } | |
228 | ||
229 | /*** FEATURE MANAGEMENT CODE ***/ | |
230 | ||
231 | struct feature { | |
232 | struct string_list *values; | |
233 | struct string_list *origins; | |
234 | struct feature *next; | |
235 | char name[1]; | |
236 | } *features; | |
237 | ||
238 | struct remapped_feature { | |
239 | const char *name; | |
240 | const char *privilege; | |
241 | int flags; /* 2 = global, 1 = local */ | |
242 | struct feature *feature; | |
243 | } remapped_features[] = { | |
244 | /* Specially handled privileges: If you change the index of | |
245 | * anything with NULL privilege, change the code in | |
246 | * finish_operators() to match! | |
247 | */ | |
248 | { "CRYPT_OPER_PASSWORD", NULL, 0, 0 }, /* default: true */ | |
249 | { "OPER_KILL", NULL, 2, 0 }, /* default: true */ | |
250 | { "LOCAL_KILL_ONLY", NULL, 2, 0 }, /* default: false */ | |
251 | /* remapped features that affect all opers */ | |
252 | { "OPER_NO_CHAN_LIMIT", "chan_limit", 3, 0 }, | |
253 | { "OPER_MODE_LCHAN", "mode_lchan", 3, 0 }, | |
254 | { "OPER_WALK_THROUGH_LMODES", "walk_lchan", 3, 0 }, | |
255 | { "NO_OPER_DEOP_LCHAN", "deop_lchan", 3, 0 }, | |
256 | { "SHOW_INVISIBLE_USERS", "show_invis", 3, 0 }, | |
257 | { "SHOW_ALL_INVISIBLE_USERS", "show_all_invis", 3, 0 }, | |
258 | { "UNLIMIT_OPER_QUERY", "unlimit_query", 3, 0 }, | |
259 | /* remapped features affecting only global opers */ | |
260 | { "OPER_REHASH", "rehash", 2, 0 }, | |
261 | { "OPER_RESTART", "restart", 2, 0 }, | |
262 | { "OPER_DIE", "die", 2, 0 }, | |
263 | { "OPER_GLINE", "gline", 2, 0 }, | |
264 | { "OPER_LGLINE", "local_gline", 2, 0 }, | |
265 | { "OPER_JUPE", "jupe", 2, 0 }, | |
266 | { "OPER_LJUPE", "local_jupe", 2, 0 }, | |
267 | { "OPER_OPMODE", "opmode", 2, 0 }, | |
268 | { "OPER_LOPMODE", "local_opmode", 2, 0 }, | |
269 | { "OPER_FORCE_OPMODE", "force_opmode", 2, 0 }, | |
270 | { "OPER_FORCE_LOPMODE", "force_local_opmode", 2, 0 }, | |
271 | { "OPER_BADCHAN", "badchan", 2, 0 }, | |
272 | { "OPER_LBADCHAN", "local_badchan", 2, 0 }, | |
273 | { "OPER_SET", "set", 2, 0 }, | |
274 | { "OPER_WIDE_GLINE", "wide_gline", 2, 0 }, | |
275 | /* remapped features affecting only local opers */ | |
276 | { "LOCOP_KILL", "kill", 1, 0 }, | |
277 | { "LOCOP_REHASH", "rehash", 1, 0 }, | |
278 | { "LOCOP_RESTART", "restart", 1, 0 }, | |
279 | { "LOCOP_DIE", "die", 1, 0 }, | |
280 | { "LOCOP_LGLINE", "local_gline", 1, 0 }, | |
281 | { "LOCOP_LJUPE", "local_jupe", 1, 0 }, | |
282 | { "LOCOP_LOPMODE", "local_opmode", 1, 0 }, | |
283 | { "LOCOP_FORCE_LOPMODE", "force_local_opmode", 1, 0 }, | |
284 | { "LOCOP_LBADCHAN", "local_badchan", 1, 0 }, | |
285 | { "LOCOP_WIDE_GLINE", "wide_gline", 1, 0 }, | |
286 | { 0, 0, 0, 0 } | |
287 | }; | |
288 | ||
289 | static void do_feature(void) | |
290 | { | |
291 | struct feature *feat; | |
292 | size_t ii; | |
293 | ||
294 | ii = strlen(fields[0]); | |
295 | feat = calloc(1, sizeof(*feat) + ii); | |
296 | while (ii-- > 0) | |
297 | feat->name[ii] = toupper(fields[0][ii]); | |
298 | feat->next = features; | |
299 | features = feat; | |
300 | string_get(&feat->origins, orig_line); | |
301 | for (ii = 1; fields[ii] && fields[ii][0]; ++ii) | |
302 | string_get(&feat->values, fields[ii]); | |
303 | } | |
304 | ||
305 | static void finish_features(void) | |
306 | { | |
307 | struct remapped_feature *rmf; | |
308 | struct string_list *sl; | |
309 | struct feature *feat; | |
310 | size_t ii; | |
311 | ||
312 | fputs("Features {\n\t\"OPLEVELS\" = \"FALSE\";\n", stdout); | |
313 | ||
314 | for (feat = features; feat; feat = feat->next) { | |
315 | /* See if the feature was remapped to an oper privilege. */ | |
316 | for (rmf = remapped_features; rmf->name; rmf++) | |
317 | if (0 == strcmp(feat->name, rmf->name)) | |
318 | break; | |
319 | if (rmf->name) { | |
320 | rmf->feature = feat; | |
321 | fprintf(stdout, "# Above feature mapped to an oper privilege.\n"); | |
322 | continue; | |
323 | } | |
324 | ||
325 | /* Was it removed? */ | |
326 | for (ii = 0; removed_features[ii]; ++ii) | |
327 | if (0 == strcmp(feat->name, removed_features[ii])) | |
328 | break; | |
329 | if (removed_features[ii]) { | |
330 | fprintf(stdout, "# Above feature no longer exists.\n"); | |
331 | continue; | |
332 | } | |
333 | ||
334 | /* Wasn't remapped, wasn't removed: print it out. */ | |
335 | fprintf(stdout, "\t\"%s\" =", feat->name); | |
336 | for (sl = feat->values; sl; sl = sl->next) | |
337 | fprintf(stdout, " \"%s\"", sl->value); | |
338 | fprintf(stdout, ";\n"); | |
339 | } | |
340 | fputs("};\n\n", stdout); | |
341 | ||
342 | } | |
343 | ||
344 | /*** OPERATOR BLOCKS ***/ | |
345 | ||
346 | struct operator { | |
347 | char *name; | |
348 | char *host; | |
349 | char *password; | |
350 | char *class; | |
351 | char *origin; | |
352 | int is_local; | |
353 | struct operator *next; | |
354 | } *operators; | |
355 | ||
356 | static void do_operator(int is_local) | |
357 | { | |
358 | struct operator *oper; | |
359 | ||
360 | oper = calloc(1, sizeof(*oper)); | |
361 | dupstring(oper->host, fields[0]); | |
362 | dupstring(oper->password, fields[1]); | |
363 | dupstring(oper->name, fields[2]); | |
364 | dupstring(oper->class, fields[4]); | |
365 | dupstring(oper->origin, orig_line); | |
366 | oper->is_local = is_local; | |
367 | oper->next = operators; | |
368 | operators = oper; | |
369 | } | |
370 | ||
371 | static void finish_operators(void) | |
372 | { | |
373 | struct remapped_feature *remap; | |
374 | struct operator *oper; | |
375 | struct feature *feat; | |
376 | char *pw_salt = ""; | |
377 | int global_kill = 0, mask = 0; | |
378 | size_t ii; | |
379 | ||
380 | if ((feat = remapped_features[0].feature) && feat->values | |
381 | && 0 == strcmp(feat->values->value, "FALSE")) | |
382 | pw_salt = "$PLAIN$"; | |
383 | ||
384 | if ((feat = remapped_features[1].feature) && feat->values | |
385 | && 0 == strcmp(feat->values->value, "FALSE")) | |
386 | global_kill = 1; | |
387 | else if ((feat = remapped_features[2].feature) && feat->values | |
388 | && 0 == strcmp(feat->values->value, "FALSE")) | |
389 | global_kill = 2; | |
390 | ||
391 | for (oper = operators; oper; oper = oper->next) { | |
392 | fprintf(stdout, "# %s\nOperator {\n\tname = \"%s\";\n" | |
393 | "\thost = \"%s\";\n\tpassword = \"%s%s\";\n" | |
394 | "\tclass = \"%s\";\n", | |
395 | oper->origin, oper->name, oper->host, pw_salt, | |
396 | oper->password, oper->class); | |
397 | if (oper->is_local) { | |
398 | fputs("\tlocal = yes;\n", stdout); | |
399 | mask = 1; | |
400 | } else { | |
401 | fputs("\tlocal = no;\n", stdout); | |
402 | if (global_kill == 1) | |
403 | fputs("\tkill = no;\n\tlocal_kill = no;\n", stdout); | |
404 | else if (global_kill == 2) | |
405 | fputs("\tkill = no;\n\tlocal_kill = yes;\n", stdout); | |
406 | mask = 2; | |
407 | } | |
408 | for (ii = 0; (remap = &remapped_features[ii++])->name; ) { | |
409 | if (!remap->feature || !remap->privilege | |
410 | || !remap->feature->values || !remap->flags & mask) | |
411 | continue; | |
412 | fprintf(stdout, "\t%s = %s;\n", remap->privilege, | |
413 | strcmp(remap->feature->values->value, "TRUE") ? "no" : "yes"); | |
414 | } | |
415 | fputs("};\n\n", stdout); | |
416 | } | |
417 | } | |
418 | ||
419 | /*** OTHER CONFIG TRANSFORMS ***/ | |
420 | ||
421 | static void do_kill(void) | |
422 | { | |
423 | const char *host = fields[0], *reason = fields[1], *user = fields[2]; | |
424 | ||
425 | if (!memcmp(host, "$R", 3)) { | |
426 | fprintf(stderr, "Empty realname K: line at line %u.\n", lineno); | |
427 | return; | |
428 | } | |
429 | ||
430 | /* Print the current line and start the new block. */ | |
431 | fprintf(stdout, "# %s\nKill {\n", orig_line); | |
432 | ||
433 | /* Translate the user-matching portions. */ | |
434 | if (host[0] == '$' && host[1] == 'R') { | |
435 | /* Realname kill, possibly with a username */ | |
436 | fprintf(stdout, "\trealname = \"%s\";\n", host + 2); | |
437 | if (user[0] != '\0' && (user[0] != '*' || user[1] != '\0')) | |
438 | fprintf(stdout, "\thost = \"%s@*\";\n", user); | |
439 | } else { | |
440 | /* Normal host or IP-based kill */ | |
441 | if (user[0] != '\0' && (user[0] != '*' || user[1] != '\0')) | |
442 | fprintf(stdout, "\thost = \"%s@%s\";\n", user, host); | |
443 | else | |
444 | fprintf(stdout, "\thost = \"%s\";\n", host); | |
445 | } | |
446 | ||
447 | /* Translate the reason section. */ | |
448 | if (reason[0] == '!') | |
449 | fprintf(stdout, "\tfile = \"%s\";\n", reason + 1); | |
450 | else | |
451 | fprintf(stdout, "\treason = \"%s\";\n", reason); | |
452 | ||
453 | /* Close the block. */ | |
454 | fprintf(stdout, "};\n"); | |
455 | } | |
456 | ||
457 | static void do_port(void) | |
458 | { | |
459 | const char *ipmask = fields[0], *iface = fields[1], *flags = fields[2], *port = fields[3]; | |
460 | ||
461 | /* Print the current line and start the new block. */ | |
462 | fprintf(stdout, "# %s\nPort {\n", orig_line); | |
463 | ||
464 | /* Print the easy fields. */ | |
465 | fprintf(stdout, "\tport = %s;\n", port); | |
466 | if (iface && iface[0] != '\0') | |
467 | fprintf(stdout, "\tvhost = \"%s\";\n", iface); | |
468 | if (ipmask && ipmask[0] != '\0') | |
469 | fprintf(stdout, "\tmask = \"%s\";\n", ipmask); | |
470 | ||
471 | /* Translate flag field. */ | |
472 | while (*flags) switch (*flags++) { | |
473 | case 'C': case 'c': /* client port is default state */; break; | |
474 | case 'S': case 's': fprintf(stdout, "\tserver = yes;\n"); break; | |
475 | case 'H': case 'h': fprintf(stdout, "\thidden = yes;\n"); break; | |
476 | } | |
477 | ||
478 | /* Close the block. */ | |
479 | fprintf(stdout, "};\n"); | |
480 | } | |
481 | ||
482 | struct string_list *quarantines; | |
483 | ||
484 | static void do_quarantine(void) | |
485 | { | |
486 | struct string_list *q; | |
487 | q = string_get(&quarantines, fields[0]); | |
488 | dupstring(q->origin, orig_line); | |
489 | dupstring(q->extra, fields[1]); | |
490 | } | |
491 | ||
492 | static void finish_quarantines(void) | |
493 | { | |
494 | struct string_list *sl; | |
495 | ||
496 | if (quarantines) | |
497 | { | |
498 | fputs("Quarantine {\n", stdout); | |
499 | for (sl = quarantines; sl; sl = sl->next) | |
500 | fprintf(stdout, "# %s\n\t\"%s\" = \"%s\";\n", sl->origin, sl->value, sl->extra); | |
501 | fputs("};\n\n", stdout); | |
502 | } | |
503 | } | |
504 | ||
505 | static void do_uworld(void) | |
506 | { | |
507 | fprintf(stdout, "# %s\n", orig_line); | |
508 | if (fields[0] && fields[0][0]) | |
509 | fprintf(stdout, "Uworld { name = \"%s\"; };\n", fields[0]); | |
510 | if (fields[1] && fields[1][0]) | |
511 | fprintf(stdout, "Jupe { nick = \"%s\"; };\n", fields[1]); | |
512 | } | |
513 | ||
514 | static void emit_client(const char *mask, const char *passwd, const char *class, long maxlinks, int is_ip) | |
515 | { | |
516 | char *delim; | |
517 | size_t len; | |
518 | ||
519 | delim = strchr(mask, '@'); | |
520 | if (delim) { | |
521 | *delim++ = '\0'; | |
522 | if (is_ip) { | |
523 | len = strspn(delim, "0123456789.*"); | |
524 | if (delim[len]) { | |
525 | fprintf(stderr, "Invalid IP mask on line %u.\n", lineno); | |
526 | return; | |
527 | } | |
528 | fprintf(stdout, "Client {\n\tusername = \"%s\";\n\tip = \"%s\";\n", mask, delim); | |
529 | } else { | |
530 | fprintf(stdout, "Client {\n\tusername =\"%s\";\n\thost = \"%s\";\n", mask, delim); | |
531 | } | |
532 | } else if (is_ip) { | |
533 | len = strspn(mask, "0123456789.*"); | |
534 | if (mask[len]) | |
535 | return; | |
536 | fprintf(stdout, "Client {\n\tip = \"%s\";\n", mask); | |
537 | } else { | |
538 | if (!strchr(mask, '.') && !strchr(mask, '*')) | |
539 | return; | |
540 | fprintf(stdout, "Client {\n\thost = \"%s\";\n", mask); | |
541 | } | |
542 | ||
543 | if (passwd) | |
544 | fprintf(stdout, "\tpassword = \"%s\";\n", passwd); | |
545 | ||
546 | if (maxlinks >= 0) | |
547 | fprintf(stdout, "\tmaxlinks = %ld;\n", maxlinks); | |
548 | ||
549 | fprintf(stdout, "\tclass = \"%s\";\n};\n", class); | |
550 | } | |
551 | ||
552 | static void do_client(void) | |
553 | { | |
554 | char *passwd = NULL, *delim; | |
555 | long maxlinks; | |
556 | ||
557 | /* Print the current line. */ | |
558 | fprintf(stdout, "# %s\n", orig_line); | |
559 | ||
560 | /* See if the password is really a maxlinks count. */ | |
561 | maxlinks = strtol(fields[1], &delim, 10); | |
562 | if (fields[1][0] == '\0') | |
563 | maxlinks = -1; | |
564 | else if (maxlinks < 0 || maxlinks > 99 || *delim != '\0') | |
565 | passwd = fields[1]; | |
566 | ||
567 | /* Translate the IP and host mask fields into blocks. */ | |
568 | emit_client(fields[0], passwd, fields[4], maxlinks, 1); | |
569 | emit_client(fields[2], passwd, fields[4], maxlinks, 0); | |
570 | } | |
571 | ||
572 | int main(int argc, char *argv[]) | |
573 | { | |
574 | FILE *ifile; | |
575 | ||
576 | if (argc < 2) | |
577 | ifile = stdin; | |
578 | else if (!(ifile = fopen(argv[1], "rt"))) { | |
579 | fprintf(stderr, "Unable to open file %s for input.\n", argv[1]); | |
580 | return 1; | |
581 | } | |
582 | ||
583 | for (lineno = 1; fgets(line, sizeof(line), ifile); ++lineno) { | |
584 | /* Read line and pass comments through. */ | |
585 | size_t len = strlen(line); | |
586 | if (line[0] == '#') { | |
587 | fputs(line, stdout); | |
588 | continue; | |
589 | } | |
590 | /* Strip EOL character(s) and pass blank lines through. */ | |
591 | while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) | |
592 | line[--len] = '\0'; | |
593 | if (len == 0) { | |
594 | fputc('\n', stdout); | |
595 | continue; | |
596 | } | |
597 | /* Skip but report invalid lines. */ | |
598 | if (line[1] != ':') { | |
599 | fprintf(stdout, "# %s\n", line); | |
600 | fprintf(stderr, "Invalid input line %d.\n", lineno); | |
601 | continue; | |
602 | } | |
603 | /* Copy the original line into a reusable variable. */ | |
604 | strcpy(orig_line, line); | |
605 | /* Split line into fields. */ | |
606 | nfields = split_line(line + 2, fields); | |
607 | ||
608 | /* Process the input line. */ | |
609 | switch (line[0]) { | |
610 | case 'A': case 'a': simple_line("Admin", admin_names, NULL); break; | |
611 | case 'C': case 'c': do_connect(); break; | |
612 | case 'D': simple_line("CRule", crule_names, "all = yes;"); break; | |
613 | case 'd': simple_line("CRule", crule_names, NULL); break; | |
614 | case 'F': case 'f': do_feature(); break; | |
615 | case 'H': case 'h': do_hub(); break; | |
616 | case 'I': case 'i': do_client(); break; | |
617 | case 'K': case 'k': do_kill(); break; | |
618 | case 'L': case 'l': do_leaf(); break; | |
619 | case 'M': case 'm': simple_line("General", general_names, NULL); break; | |
620 | case 'O': do_operator(0); break; | |
621 | case 'o': do_operator(1); break; | |
622 | case 'P': case 'p': do_port(); break; | |
623 | case 'Q': case 'q': do_quarantine(); break; | |
624 | case 'T': case 't': simple_line("Motd", motd_names, NULL); break; | |
625 | case 'U': case 'u': do_uworld(); break; | |
626 | case 'Y': case 'y': simple_line("Class", class_names, NULL); break; | |
627 | default: | |
628 | fprintf(stderr, "Unknown line %u with leading character '%c'.\n", lineno, line[0]); | |
629 | break; | |
630 | } | |
631 | } | |
632 | ||
633 | fclose(ifile); | |
634 | ||
635 | fputs("\n# The following lines were intentionally moved and rearranged." | |
636 | "\n# Our apologies for any inconvenience this may cause." | |
637 | "\n\n", stdout); | |
638 | finish_connects(); | |
639 | finish_quarantines(); | |
640 | finish_features(); | |
641 | finish_operators(); | |
642 | ||
643 | return 0; | |
644 | } |