]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
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" | |
212380e3 AC |
28 | #include "s_log.h" |
29 | #include "s_conf.h" | |
30 | #include "s_newconf.h" | |
31 | #include "hostmask.h" | |
32 | #include "client.h" | |
33 | #include "irc_string.h" | |
212380e3 AC |
34 | #include "hash.h" |
35 | ||
36 | /* conf_add_fields() | |
37 | * | |
c8de4f00 | 38 | * inputs - pointer to config item, host/pass/user/operreason/date fields |
212380e3 AC |
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, | |
c8de4f00 | 45 | const char *operreason_field, const char *date_field) |
212380e3 AC |
46 | { |
47 | if(host_field != NULL) | |
47a03750 | 48 | aconf->host = rb_strdup(host_field); |
212380e3 | 49 | if(pass_field != NULL) |
c8de4f00 JT |
50 | { |
51 | if(!EmptyString(date_field)) | |
52 | { | |
eddc2ab6 | 53 | aconf->passwd = rb_malloc(strlen(pass_field) + strlen(date_field) + 4); |
b2f0da88 | 54 | rb_sprintf(aconf->passwd, "%s (%s)", pass_field, date_field); |
c8de4f00 JT |
55 | } |
56 | else | |
47a03750 | 57 | aconf->passwd = rb_strdup(pass_field); |
c8de4f00 | 58 | } |
212380e3 | 59 | if(user_field != NULL) |
47a03750 | 60 | aconf->user = rb_strdup(user_field); |
212380e3 | 61 | if(operreason_field != NULL) |
47a03750 | 62 | aconf->spasswd = rb_strdup(operreason_field); |
212380e3 AC |
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; | |
c8de4f00 | 80 | char *date_field = NULL; |
212380e3 AC |
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); | |
c8de4f00 | 105 | date_field = getfield(NULL); |
212380e3 AC |
106 | |
107 | aconf = make_conf(); | |
108 | aconf->status = CONF_KILL; | |
109 | conf_add_fields(aconf, host_field, reason_field, | |
c8de4f00 | 110 | user_field, operreason_field, date_field); |
212380e3 AC |
111 | |
112 | if(aconf->host != NULL) | |
113 | add_conf_by_address(aconf->host, CONF_KILL, aconf->user, 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; | |
c8de4f00 | 131 | char *date_field = NULL; |
212380e3 AC |
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); | |
c8de4f00 | 152 | date_field = getfield(NULL); |
212380e3 AC |
153 | |
154 | aconf = make_conf(); | |
155 | aconf->status = CONF_DLINE; | |
c8de4f00 | 156 | conf_add_fields(aconf, host_field, reason_field, "", operreason_field, date_field); |
212380e3 AC |
157 | conf_add_d_conf(aconf); |
158 | } | |
159 | } | |
160 | ||
161 | void | |
162 | parse_x_file(FILE * file) | |
163 | { | |
164 | struct ConfItem *aconf; | |
165 | char *gecos_field = NULL; | |
166 | char *reason_field = NULL; | |
167 | char line[BUFSIZE]; | |
168 | char *p; | |
169 | ||
170 | while (fgets(line, sizeof(line), file)) | |
171 | { | |
172 | if((p = strchr(line, '\n'))) | |
173 | *p = '\0'; | |
174 | ||
175 | if((*line == '\0') || (line[0] == '#')) | |
176 | continue; | |
177 | ||
178 | gecos_field = getfield(line); | |
179 | if(EmptyString(gecos_field)) | |
180 | continue; | |
181 | ||
182 | /* field for xline types, which no longer exist */ | |
183 | getfield(NULL); | |
184 | ||
185 | reason_field = getfield(NULL); | |
186 | if(EmptyString(reason_field)) | |
187 | continue; | |
188 | ||
189 | /* sanity checking */ | |
0fdb2570 | 190 | if((find_xline_mask(gecos_field) != NULL) || |
212380e3 AC |
191 | (strchr(reason_field, ':') != NULL)) |
192 | continue; | |
193 | ||
194 | aconf = make_conf(); | |
195 | aconf->status = CONF_XLINE; | |
196 | ||
47a03750 VY |
197 | aconf->name = rb_strdup(gecos_field); |
198 | aconf->passwd = rb_strdup(reason_field); | |
212380e3 | 199 | |
330fc5c1 | 200 | rb_dlinkAddAlloc(aconf, &xline_conf_list); |
212380e3 AC |
201 | } |
202 | } | |
203 | ||
204 | void | |
205 | parse_resv_file(FILE * file) | |
206 | { | |
207 | struct ConfItem *aconf; | |
208 | char *reason_field; | |
209 | char *host_field; | |
210 | char line[BUFSIZE]; | |
211 | char *p; | |
212 | ||
213 | while (fgets(line, sizeof(line), file)) | |
214 | { | |
215 | if((p = strchr(line, '\n'))) | |
216 | *p = '\0'; | |
217 | ||
218 | if((*line == '\0') || (line[0] == '#')) | |
219 | continue; | |
220 | ||
221 | host_field = getfield(line); | |
222 | if(EmptyString(host_field)) | |
223 | continue; | |
224 | ||
225 | reason_field = getfield(NULL); | |
226 | if(EmptyString(reason_field)) | |
227 | continue; | |
228 | ||
229 | if(IsChannelName(host_field)) | |
230 | { | |
231 | if(hash_find_resv(host_field)) | |
232 | continue; | |
233 | ||
234 | aconf = make_conf(); | |
235 | aconf->status = CONF_RESV_CHANNEL; | |
236 | aconf->port = 0; | |
237 | ||
47a03750 VY |
238 | aconf->name = rb_strdup(host_field); |
239 | aconf->passwd = rb_strdup(reason_field); | |
212380e3 AC |
240 | add_to_resv_hash(aconf->name, aconf); |
241 | } | |
242 | else if(clean_resv_nick(host_field)) | |
243 | { | |
0fdb2570 | 244 | if(find_nick_resv_mask(host_field)) |
212380e3 AC |
245 | continue; |
246 | ||
247 | aconf = make_conf(); | |
248 | aconf->status = CONF_RESV_NICK; | |
249 | aconf->port = 0; | |
250 | ||
47a03750 VY |
251 | aconf->name = rb_strdup(host_field); |
252 | aconf->passwd = rb_strdup(reason_field); | |
330fc5c1 | 253 | rb_dlinkAddAlloc(aconf, &resv_conf_list); |
212380e3 AC |
254 | } |
255 | } | |
256 | } | |
257 | ||
258 | /* | |
259 | * getfield | |
260 | * | |
261 | * inputs - input buffer | |
262 | * output - next field | |
263 | * side effects - field breakup for ircd.conf file. | |
264 | */ | |
265 | char * | |
266 | getfield(char *newline) | |
267 | { | |
268 | static char *line = NULL; | |
269 | char *end, *field; | |
270 | ||
271 | if(newline != NULL) | |
272 | line = newline; | |
273 | ||
274 | if(line == NULL) | |
275 | return (NULL); | |
276 | ||
277 | field = line; | |
278 | ||
279 | /* XXX make this skip to first " if present */ | |
280 | if(*field == '"') | |
281 | field++; | |
282 | else | |
283 | return (NULL); /* mal-formed field */ | |
284 | ||
285 | end = strchr(line, ','); | |
286 | ||
287 | while(1) | |
288 | { | |
289 | /* no trailing , - last field */ | |
290 | if(end == NULL) | |
291 | { | |
292 | end = line + strlen(line); | |
293 | line = NULL; | |
294 | ||
295 | if(*end == '"') | |
296 | { | |
297 | *end = '\0'; | |
298 | return field; | |
299 | } | |
300 | else | |
301 | return NULL; | |
302 | } | |
303 | else | |
304 | { | |
305 | /* look for a ", to mark the end of a field.. */ | |
306 | if(*(end - 1) == '"') | |
307 | { | |
308 | line = end + 1; | |
309 | end--; | |
310 | *end = '\0'; | |
311 | return field; | |
312 | } | |
313 | ||
314 | /* search for the next ',' */ | |
315 | end++; | |
316 | end = strchr(end, ','); | |
317 | } | |
318 | } | |
319 | ||
320 | return NULL; | |
321 | } |