]> jfr.im git - irc/rqf/shadowircd.git/blob - src/kdparse.c
Change xlines to store spaces as \s.
[irc/rqf/shadowircd.git] / src / kdparse.c
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"
28 #include "logger.h"
29 #include "s_conf.h"
30 #include "s_newconf.h"
31 #include "hostmask.h"
32 #include "client.h"
33 #include "match.h"
34 #include "hash.h"
35
36 /* conf_add_fields()
37 *
38 * inputs - pointer to config item, host/pass/user/operreason/date fields
39 * output - NONE
40 * side effects - update respective fields with pointers
41 */
42 static void
43 conf_add_fields(struct ConfItem *aconf, const char *host_field,
44 const char *pass_field, const char *user_field,
45 const char *operreason_field, const char *date_field)
46 {
47 if(host_field != NULL)
48 aconf->host = rb_strdup(host_field);
49 if(pass_field != NULL)
50 {
51 if(!EmptyString(date_field))
52 {
53 aconf->passwd = rb_malloc(strlen(pass_field) + strlen(date_field) + 4);
54 rb_sprintf(aconf->passwd, "%s (%s)", pass_field, date_field);
55 }
56 else
57 aconf->passwd = rb_strdup(pass_field);
58 }
59 if(user_field != NULL)
60 aconf->user = rb_strdup(user_field);
61 if(operreason_field != NULL)
62 aconf->spasswd = rb_strdup(operreason_field);
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
72 void
73 parse_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;
80 char *date_field = NULL;
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);
105 date_field = getfield(NULL);
106
107 aconf = make_conf();
108 aconf->status = CONF_KILL;
109 conf_add_fields(aconf, host_field, reason_field,
110 user_field, operreason_field, date_field);
111
112 if(aconf->host != NULL)
113 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
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
124 void
125 parse_d_file(FILE * file)
126 {
127 struct ConfItem *aconf;
128 char *reason_field = NULL;
129 char *host_field = NULL;
130 char *operreason_field = NULL;
131 char *date_field = NULL;
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);
152 date_field = getfield(NULL);
153
154 aconf = make_conf();
155 aconf->status = CONF_DLINE;
156 conf_add_fields(aconf, host_field, reason_field, "", operreason_field, date_field);
157 conf_add_d_conf(aconf);
158 }
159 }
160
161 char *
162 xline_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
192 void
193 parse_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;
200 char *encoded;
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 */
222 if((find_xline_mask(gecos_field) != NULL) ||
223 (strchr(reason_field, ':') != NULL))
224 continue;
225
226 encoded = xline_encode_spaces(gecos_field);
227 if (find_xline_mask(encoded) != NULL)
228 {
229 rb_free(encoded);
230 continue;
231 }
232
233 aconf = make_conf();
234 aconf->status = CONF_XLINE;
235
236 aconf->name = encoded;
237 aconf->passwd = rb_strdup(reason_field);
238
239 rb_dlinkAddAlloc(aconf, &xline_conf_list);
240 }
241 }
242
243 void
244 parse_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
277 aconf->name = rb_strdup(host_field);
278 aconf->passwd = rb_strdup(reason_field);
279 add_to_resv_hash(aconf->name, aconf);
280 }
281 else if(clean_resv_nick(host_field))
282 {
283 if(find_nick_resv_mask(host_field))
284 continue;
285
286 aconf = make_conf();
287 aconf->status = CONF_RESV_NICK;
288 aconf->port = 0;
289
290 aconf->name = rb_strdup(host_field);
291 aconf->passwd = rb_strdup(reason_field);
292 rb_dlinkAddAlloc(aconf, &resv_conf_list);
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 */
304 char *
305 getfield(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 }