]> jfr.im git - irc/rqf/shadowircd.git/blob - tools/convertklines.c
Removal of ancient SVN ID's part one
[irc/rqf/shadowircd.git] / tools / convertklines.c
1 /************************************************************************
2 * IRC - Internet Relay Chat, tools/convertklines.c
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 1, or (at your option)
7 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <string.h>
24
25 #define BUFSIZE 512
26
27 static void ConvertConf(FILE* file,FILE *outkline, FILE *outdline);
28 static void usage(void);
29 static char *getfield(char *);
30 static void ReplaceQuotes(char *out, char *in);
31 static void parse(FILE *outkline, FILE *outdline, char *in);
32
33 int main(int argc,char *argv[])
34 {
35 FILE *in;
36 FILE *outkline;
37 FILE *outdline;
38
39 if(argc < 4)
40 usage();
41
42 if (( in = fopen(argv[1],"r")) == NULL )
43 {
44 fprintf(stderr,"Can't open %s for reading\n", argv[1]);
45 usage();
46 }
47
48 if (( outkline = fopen(argv[2],"w")) == NULL )
49 {
50 fprintf(stderr,"Can't open %s for writing\n", argv[2]);
51 usage();
52 }
53
54 if(( outdline = fopen(argv[3], "w")) == NULL )
55 {
56 fprintf(stderr, "Can't open %s for writing\n", argv[3]);
57 usage();
58 }
59
60 ConvertConf(in, outkline, outdline);
61
62 fprintf(stderr, "The kline file has been converted and should be renamed to\n");
63 fprintf(stderr, "the config.h options (normally kline.conf and dline.conf) and\n");
64 fprintf(stderr, "placed in your etc/ dir\n");
65 return 0;
66 }
67
68 static void usage()
69 {
70 fprintf(stderr, "klines and dlines now go in separate files:\n");
71 fprintf(stderr,"convertklines kline.conf.old kline.conf.new dline.conf.new\n");
72 exit(-1);
73 }
74
75
76 /*
77 * ConvertConf()
78 * Read configuration file.
79 *
80 *
81 * inputs - FILE* to config file to convert
82 * - FILE* to output for new klines
83 * - FILE* to output for new dlines
84 * outputs - -1 if the file cannot be opened
85 * - 0 otherwise
86 */
87
88 static void ConvertConf(FILE* file, FILE *outkline, FILE *outdline)
89 {
90 char line[BUFSIZE];
91 char quotedLine[BUFSIZE];
92 char* p;
93
94 while (fgets(line, sizeof(line), file))
95 {
96 if ((p = strchr(line, '\n')))
97 *p = '\0';
98
99 ReplaceQuotes(quotedLine,line);
100
101 if (!*quotedLine || quotedLine[0] == '#' || quotedLine[0] == '\n' ||
102 quotedLine[0] == ' ' || quotedLine[0] == '\t')
103 continue;
104
105 /* Could we test if it's conf line at all? -Vesa */
106 if (quotedLine[1] == ':')
107 {
108 parse(outkline, outdline, quotedLine);
109 }
110 }
111
112 fclose(file);
113 }
114
115 /*
116 * ReplaceQuotes
117 * Inputs - input line to quote
118 * Output - quoted line
119 * Side Effects - All quoted chars in input are replaced
120 * with quoted values in output, # chars replaced with '\0'
121 * otherwise input is copied to output.
122 */
123 static void ReplaceQuotes(char* quotedLine,char *inputLine)
124 {
125 char *in;
126 char *out;
127 static char quotes[] = {
128 0, /* */
129 0, /* a */
130 '\b', /* b */
131 0, /* c */
132 0, /* d */
133 0, /* e */
134 '\f', /* f */
135 0, /* g */
136 0, /* h */
137 0, /* i */
138 0, /* j */
139 0, /* k */
140 0, /* l */
141 0, /* m */
142 '\n', /* n */
143 0, /* o */
144 0, /* p */
145 0, /* q */
146 '\r', /* r */
147 0, /* s */
148 '\t', /* t */
149 0, /* u */
150 '\v', /* v */
151 0, /* w */
152 0, /* x */
153 0, /* y */
154 0, /* z */
155 0,0,0,0,0,0
156 };
157
158 /*
159 * Do quoting of characters and # detection.
160 */
161 for (out = quotedLine,in = inputLine; *in; out++, in++)
162 {
163 if (*in == '\\')
164 {
165 in++;
166 if(*in == '\\')
167 *out = '\\';
168 else if(*in == '#')
169 *out = '#';
170 else
171 *out = quotes[ (unsigned int) (*in & 0x1F) ];
172 }
173 else if (*in == '#')
174 {
175 *out = '\0';
176 return;
177 }
178 else
179 *out = *in;
180 }
181 *out = '\0';
182 }
183
184 /*
185 * parse()
186 * Inputs - pointer to line to parse
187 * - pointer to output to write
188 * Output -
189 * Side Effects - Parse one old style conf line.
190 */
191
192 static void parse(FILE *outkline, FILE *outdline, char* line)
193 {
194 char conf_letter;
195 char *tmp;
196 const char *user_field = NULL;
197 const char *passwd_field = NULL;
198 const char *host_field = NULL;
199 const char *operpasswd_field = NULL;
200
201 tmp = getfield(line);
202
203 conf_letter = *tmp;
204
205 for (;;) /* Fake loop, that I can use break here --msa */
206 {
207 /* host field */
208 if ((host_field = getfield(NULL)) == NULL)
209 return;
210
211 /* pass field */
212 if ((passwd_field = getfield(NULL)) == NULL)
213 break;
214 else
215 {
216 /* if theres a password, try splitting the operreason out */
217 char *p;
218
219 if((p = strchr(passwd_field, '|')))
220 {
221 *p++ = '\0';
222 operpasswd_field = p;
223 }
224 else
225 operpasswd_field = "";
226 }
227
228 /* user field */
229 if ((user_field = getfield(NULL)) == NULL)
230 break;
231
232 /* what could possibly be after a userfield? */
233 break;
234 }
235
236 if (!passwd_field)
237 {
238 passwd_field = "";
239 operpasswd_field = "";
240 }
241
242 if (!user_field)
243 user_field = "";
244
245 switch( conf_letter )
246 {
247 case 'd':
248 fprintf(stderr, "exempt in old file, ignoring.\n");
249 break;
250
251 case 'D': /* Deny lines (immediate refusal) */
252 if(host_field && passwd_field)
253 fprintf(outdline, "\"%s\",\"%s\",\"%s\",\"\",\"Unknown\",0\n",
254 host_field, passwd_field, operpasswd_field);
255 break;
256
257 case 'K': /* Kill user line on irc.conf */
258 case 'k':
259 if(host_field && passwd_field && user_field)
260 fprintf(outkline, "\"%s\",\"%s\",\"%s\",\"%s\",\"\",\"Unknown\",0\n",
261 user_field, host_field, passwd_field, operpasswd_field);
262 break;
263
264 default:
265 fprintf(stderr, "Error in config file: %s", line);
266 break;
267 }
268 }
269
270
271 /*
272 * field breakup for ircd.conf file.
273 */
274 static char *getfield(char *newline)
275 {
276 static char *line = NULL;
277 char *end, *field;
278
279 if (newline)
280 line = newline;
281
282 if (line == NULL)
283 {
284 fprintf(stderr, "returned null!\n");
285 return NULL;
286 }
287
288 field = line;
289
290 if ((end = strchr(line,':')) == NULL)
291 {
292 line = NULL;
293 if ((end = strchr(field,'\n')) == NULL)
294 end = field + strlen(field);
295 }
296 else
297 line = end + 1;
298
299 *end = '\0';
300
301 return field;
302 }
303
304