]> jfr.im git - irc/rqf/shadowircd.git/blob - src/kdparse.c
ircs[n]printf -> rb_s[n]printf
[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 "tools.h"
29 #include "s_log.h"
30 #include "s_conf.h"
31 #include "s_newconf.h"
32 #include "hostmask.h"
33 #include "client.h"
34 #include "irc_string.h"
35 #include "memory.h"
36 #include "hash.h"
37
38 /* conf_add_fields()
39 *
40 * inputs - pointer to config item, host/pass/user/operreason/date fields
41 * output - NONE
42 * side effects - update respective fields with pointers
43 */
44 static void
45 conf_add_fields(struct ConfItem *aconf, const char *host_field,
46 const char *pass_field, const char *user_field,
47 const char *operreason_field, const char *date_field)
48 {
49 if(host_field != NULL)
50 DupString(aconf->host, host_field);
51 if(pass_field != NULL)
52 {
53 if(!EmptyString(date_field))
54 {
55 aconf->passwd = MyMalloc(strlen(pass_field) + strlen(date_field) + 4);
56 rb_sprintf(aconf->passwd, "%s (%s)", pass_field, date_field);
57 }
58 else
59 DupString(aconf->passwd, pass_field);
60 }
61 if(user_field != NULL)
62 DupString(aconf->user, user_field);
63 if(operreason_field != NULL)
64 DupString(aconf->spasswd, operreason_field);
65 }
66
67 /*
68 * parse_k_file
69 * Inputs - pointer to line to parse
70 * Output - NONE
71 * Side Effects - Parse one new style K line
72 */
73
74 void
75 parse_k_file(FILE * file)
76 {
77 struct ConfItem *aconf;
78 char *user_field = NULL;
79 char *reason_field = NULL;
80 char *operreason_field = NULL;
81 char *host_field = NULL;
82 char *date_field = NULL;
83 char line[BUFSIZE];
84 char *p;
85
86 while (fgets(line, sizeof(line), file))
87 {
88 if((p = strchr(line, '\n')) != NULL)
89 *p = '\0';
90
91 if((*line == '\0') || (*line == '#'))
92 continue;
93
94 user_field = getfield(line);
95 if(EmptyString(user_field))
96 continue;
97
98 host_field = getfield(NULL);
99 if(EmptyString(host_field))
100 continue;
101
102 reason_field = getfield(NULL);
103 if(EmptyString(reason_field))
104 continue;
105
106 operreason_field = getfield(NULL);
107 date_field = getfield(NULL);
108
109 aconf = make_conf();
110 aconf->status = CONF_KILL;
111 conf_add_fields(aconf, host_field, reason_field,
112 user_field, operreason_field, date_field);
113
114 if(aconf->host != NULL)
115 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
116 }
117 }
118
119 /*
120 * parse_d_file
121 * Inputs - pointer to line to parse
122 * Output - NONE
123 * Side Effects - Parse one new style D line
124 */
125
126 void
127 parse_d_file(FILE * file)
128 {
129 struct ConfItem *aconf;
130 char *reason_field = NULL;
131 char *host_field = NULL;
132 char *operreason_field = NULL;
133 char *date_field = NULL;
134 char line[BUFSIZE];
135 char *p;
136
137 while (fgets(line, sizeof(line), file))
138 {
139 if((p = strchr(line, '\n')))
140 *p = '\0';
141
142 if((*line == '\0') || (line[0] == '#'))
143 continue;
144
145 host_field = getfield(line);
146 if(EmptyString(host_field))
147 continue;
148
149 reason_field = getfield(NULL);
150 if(EmptyString(reason_field))
151 continue;
152
153 operreason_field = getfield(NULL);
154 date_field = getfield(NULL);
155
156 aconf = make_conf();
157 aconf->status = CONF_DLINE;
158 conf_add_fields(aconf, host_field, reason_field, "", operreason_field, date_field);
159 conf_add_d_conf(aconf);
160 }
161 }
162
163 void
164 parse_x_file(FILE * file)
165 {
166 struct ConfItem *aconf;
167 char *gecos_field = NULL;
168 char *reason_field = NULL;
169 char line[BUFSIZE];
170 char *p;
171
172 while (fgets(line, sizeof(line), file))
173 {
174 if((p = strchr(line, '\n')))
175 *p = '\0';
176
177 if((*line == '\0') || (line[0] == '#'))
178 continue;
179
180 gecos_field = getfield(line);
181 if(EmptyString(gecos_field))
182 continue;
183
184 /* field for xline types, which no longer exist */
185 getfield(NULL);
186
187 reason_field = getfield(NULL);
188 if(EmptyString(reason_field))
189 continue;
190
191 /* sanity checking */
192 if((find_xline_mask(gecos_field) != NULL) ||
193 (strchr(reason_field, ':') != NULL))
194 continue;
195
196 aconf = make_conf();
197 aconf->status = CONF_XLINE;
198
199 DupString(aconf->name, gecos_field);
200 DupString(aconf->passwd, reason_field);
201
202 dlinkAddAlloc(aconf, &xline_conf_list);
203 }
204 }
205
206 void
207 parse_resv_file(FILE * file)
208 {
209 struct ConfItem *aconf;
210 char *reason_field;
211 char *host_field;
212 char line[BUFSIZE];
213 char *p;
214
215 while (fgets(line, sizeof(line), file))
216 {
217 if((p = strchr(line, '\n')))
218 *p = '\0';
219
220 if((*line == '\0') || (line[0] == '#'))
221 continue;
222
223 host_field = getfield(line);
224 if(EmptyString(host_field))
225 continue;
226
227 reason_field = getfield(NULL);
228 if(EmptyString(reason_field))
229 continue;
230
231 if(IsChannelName(host_field))
232 {
233 if(hash_find_resv(host_field))
234 continue;
235
236 aconf = make_conf();
237 aconf->status = CONF_RESV_CHANNEL;
238 aconf->port = 0;
239
240 DupString(aconf->name, host_field);
241 DupString(aconf->passwd, reason_field);
242 add_to_resv_hash(aconf->name, aconf);
243 }
244 else if(clean_resv_nick(host_field))
245 {
246 if(find_nick_resv_mask(host_field))
247 continue;
248
249 aconf = make_conf();
250 aconf->status = CONF_RESV_NICK;
251 aconf->port = 0;
252
253 DupString(aconf->name, host_field);
254 DupString(aconf->passwd, reason_field);
255 dlinkAddAlloc(aconf, &resv_conf_list);
256 }
257 }
258 }
259
260 /*
261 * getfield
262 *
263 * inputs - input buffer
264 * output - next field
265 * side effects - field breakup for ircd.conf file.
266 */
267 char *
268 getfield(char *newline)
269 {
270 static char *line = NULL;
271 char *end, *field;
272
273 if(newline != NULL)
274 line = newline;
275
276 if(line == NULL)
277 return (NULL);
278
279 field = line;
280
281 /* XXX make this skip to first " if present */
282 if(*field == '"')
283 field++;
284 else
285 return (NULL); /* mal-formed field */
286
287 end = strchr(line, ',');
288
289 while(1)
290 {
291 /* no trailing , - last field */
292 if(end == NULL)
293 {
294 end = line + strlen(line);
295 line = NULL;
296
297 if(*end == '"')
298 {
299 *end = '\0';
300 return field;
301 }
302 else
303 return NULL;
304 }
305 else
306 {
307 /* look for a ", to mark the end of a field.. */
308 if(*(end - 1) == '"')
309 {
310 line = end + 1;
311 end--;
312 *end = '\0';
313 return field;
314 }
315
316 /* search for the next ',' */
317 end++;
318 end = strchr(end, ',');
319 }
320 }
321
322 return NULL;
323 }