]> jfr.im git - irc/rqf/shadowircd.git/blame - src/kdparse.c
Note that blacklist{} only accepts host/reason pairs, no host+host+reason.
[irc/rqf/shadowircd.git] / src / kdparse.c
CommitLineData
212380e3 1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * kdparse.c: Parses K and D lines.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: kdparse.c 254 2005-09-21 23:35:12Z nenolod $
25 */
26
27#include "stdinc.h"
d3455e2c 28#include "logger.h"
212380e3 29#include "s_conf.h"
30#include "s_newconf.h"
31#include "hostmask.h"
32#include "client.h"
13ae2f4b 33#include "match.h"
212380e3 34#include "hash.h"
35
36/* conf_add_fields()
37 *
5be2a08b 38 * inputs - pointer to config item, host/pass/user/operreason/date fields
212380e3 39 * output - NONE
40 * side effects - update respective fields with pointers
41 */
42static void
43conf_add_fields(struct ConfItem *aconf, const char *host_field,
44 const char *pass_field, const char *user_field,
5be2a08b 45 const char *operreason_field, const char *date_field)
212380e3 46{
47 if(host_field != NULL)
62d28946 48 aconf->host = rb_strdup(host_field);
212380e3 49 if(pass_field != NULL)
5be2a08b
JT
50 {
51 if(!EmptyString(date_field))
52 {
8e43b0b4 53 aconf->passwd = rb_malloc(strlen(pass_field) + strlen(date_field) + 4);
38e6acdd 54 rb_sprintf(aconf->passwd, "%s (%s)", pass_field, date_field);
5be2a08b
JT
55 }
56 else
62d28946 57 aconf->passwd = rb_strdup(pass_field);
5be2a08b 58 }
212380e3 59 if(user_field != NULL)
62d28946 60 aconf->user = rb_strdup(user_field);
212380e3 61 if(operreason_field != NULL)
62d28946 62 aconf->spasswd = rb_strdup(operreason_field);
212380e3 63}
64
65/*
66 * parse_k_file
67 * Inputs - pointer to line to parse
68 * Output - NONE
69 * Side Effects - Parse one new style K line
70 */
71
72void
73parse_k_file(FILE * file)
74{
75 struct ConfItem *aconf;
76 char *user_field = NULL;
77 char *reason_field = NULL;
78 char *operreason_field = NULL;
79 char *host_field = NULL;
5be2a08b 80 char *date_field = NULL;
212380e3 81 char line[BUFSIZE];
82 char *p;
83
84 while (fgets(line, sizeof(line), file))
85 {
86 if((p = strchr(line, '\n')) != NULL)
87 *p = '\0';
88
89 if((*line == '\0') || (*line == '#'))
90 continue;
91
92 user_field = getfield(line);
93 if(EmptyString(user_field))
94 continue;
95
96 host_field = getfield(NULL);
97 if(EmptyString(host_field))
98 continue;
99
100 reason_field = getfield(NULL);
101 if(EmptyString(reason_field))
102 continue;
103
104 operreason_field = getfield(NULL);
5be2a08b 105 date_field = getfield(NULL);
212380e3 106
107 aconf = make_conf();
108 aconf->status = CONF_KILL;
109 conf_add_fields(aconf, host_field, reason_field,
5be2a08b 110 user_field, operreason_field, date_field);
212380e3 111
112 if(aconf->host != NULL)
969a1ae6 113 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
212380e3 114 }
115}
116
117/*
118 * parse_d_file
119 * Inputs - pointer to line to parse
120 * Output - NONE
121 * Side Effects - Parse one new style D line
122 */
123
124void
125parse_d_file(FILE * file)
126{
127 struct ConfItem *aconf;
128 char *reason_field = NULL;
129 char *host_field = NULL;
130 char *operreason_field = NULL;
5be2a08b 131 char *date_field = NULL;
212380e3 132 char line[BUFSIZE];
133 char *p;
134
135 while (fgets(line, sizeof(line), file))
136 {
137 if((p = strchr(line, '\n')))
138 *p = '\0';
139
140 if((*line == '\0') || (line[0] == '#'))
141 continue;
142
143 host_field = getfield(line);
144 if(EmptyString(host_field))
145 continue;
146
147 reason_field = getfield(NULL);
148 if(EmptyString(reason_field))
149 continue;
150
151 operreason_field = getfield(NULL);
5be2a08b 152 date_field = getfield(NULL);
212380e3 153
154 aconf = make_conf();
155 aconf->status = CONF_DLINE;
5be2a08b 156 conf_add_fields(aconf, host_field, reason_field, "", operreason_field, date_field);
212380e3 157 conf_add_d_conf(aconf);
158 }
159}
160
63860dd1
JT
161char *
162xline_encode_spaces(const char *mask)
163{
164 int i, j;
165 int spaces = 0;
166 int backslash = 0;
167 char *encoded;
168
169 for (i = 0; mask[i] != '\0'; i++)
170 if (mask[i] == ' ')
171 spaces++;
172 encoded = rb_malloc(i + spaces + 1);
173 for (i = 0, j = 0; mask[i] != '\0'; i++)
174 {
175 if (mask[i] == '\\')
176 backslash = !backslash;
177 else if (mask[i] == ' ')
178 {
179 if (!backslash)
180 encoded[j++] = '\\';
181 encoded[j++] = 's';
182 backslash = 0;
183 continue;
184 }
185 else
186 backslash = 0;
187 encoded[j++] = mask[i];
188 }
189 return encoded;
190}
191
212380e3 192void
193parse_x_file(FILE * file)
194{
195 struct ConfItem *aconf;
196 char *gecos_field = NULL;
197 char *reason_field = NULL;
198 char line[BUFSIZE];
199 char *p;
63860dd1 200 char *encoded;
212380e3 201
202 while (fgets(line, sizeof(line), file))
203 {
204 if((p = strchr(line, '\n')))
205 *p = '\0';
206
207 if((*line == '\0') || (line[0] == '#'))
208 continue;
209
210 gecos_field = getfield(line);
211 if(EmptyString(gecos_field))
212 continue;
213
214 /* field for xline types, which no longer exist */
215 getfield(NULL);
216
217 reason_field = getfield(NULL);
218 if(EmptyString(reason_field))
219 continue;
220
221 /* sanity checking */
0fdb2570 222 if((find_xline_mask(gecos_field) != NULL) ||
212380e3 223 (strchr(reason_field, ':') != NULL))
224 continue;
225
63860dd1
JT
226 encoded = xline_encode_spaces(gecos_field);
227 if (find_xline_mask(encoded) != NULL)
228 {
229 rb_free(encoded);
230 continue;
231 }
232
212380e3 233 aconf = make_conf();
234 aconf->status = CONF_XLINE;
235
63860dd1 236 aconf->name = encoded;
62d28946 237 aconf->passwd = rb_strdup(reason_field);
212380e3 238
af81d5a0 239 rb_dlinkAddAlloc(aconf, &xline_conf_list);
212380e3 240 }
241}
242
243void
244parse_resv_file(FILE * file)
245{
246 struct ConfItem *aconf;
247 char *reason_field;
248 char *host_field;
249 char line[BUFSIZE];
250 char *p;
251
252 while (fgets(line, sizeof(line), file))
253 {
254 if((p = strchr(line, '\n')))
255 *p = '\0';
256
257 if((*line == '\0') || (line[0] == '#'))
258 continue;
259
260 host_field = getfield(line);
261 if(EmptyString(host_field))
262 continue;
263
264 reason_field = getfield(NULL);
265 if(EmptyString(reason_field))
266 continue;
267
268 if(IsChannelName(host_field))
269 {
270 if(hash_find_resv(host_field))
271 continue;
272
273 aconf = make_conf();
274 aconf->status = CONF_RESV_CHANNEL;
275 aconf->port = 0;
276
62d28946
VY
277 aconf->name = rb_strdup(host_field);
278 aconf->passwd = rb_strdup(reason_field);
212380e3 279 add_to_resv_hash(aconf->name, aconf);
280 }
281 else if(clean_resv_nick(host_field))
282 {
0fdb2570 283 if(find_nick_resv_mask(host_field))
212380e3 284 continue;
285
286 aconf = make_conf();
287 aconf->status = CONF_RESV_NICK;
288 aconf->port = 0;
289
62d28946
VY
290 aconf->name = rb_strdup(host_field);
291 aconf->passwd = rb_strdup(reason_field);
af81d5a0 292 rb_dlinkAddAlloc(aconf, &resv_conf_list);
212380e3 293 }
294 }
295}
296
297/*
298 * getfield
299 *
300 * inputs - input buffer
301 * output - next field
302 * side effects - field breakup for ircd.conf file.
303 */
304char *
305getfield(char *newline)
306{
307 static char *line = NULL;
308 char *end, *field;
309
310 if(newline != NULL)
311 line = newline;
312
313 if(line == NULL)
314 return (NULL);
315
316 field = line;
317
318 /* XXX make this skip to first " if present */
319 if(*field == '"')
320 field++;
321 else
322 return (NULL); /* mal-formed field */
323
324 end = strchr(line, ',');
325
326 while(1)
327 {
328 /* no trailing , - last field */
329 if(end == NULL)
330 {
331 end = line + strlen(line);
332 line = NULL;
333
334 if(*end == '"')
335 {
336 *end = '\0';
337 return field;
338 }
339 else
340 return NULL;
341 }
342 else
343 {
344 /* look for a ", to mark the end of a field.. */
345 if(*(end - 1) == '"')
346 {
347 line = end + 1;
348 end--;
349 *end = '\0';
350 return field;
351 }
352
353 /* search for the next ',' */
354 end++;
355 end = strchr(end, ',');
356 }
357 }
358
359 return NULL;
360}