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