shadowircd / src / bandbi.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* src/bandbi.c
 * An interface to the ban db.
 *
 * Copyright (C) 2006 Lee Hardy <lee -at- leeh.co.uk>
 * Copyright (C) 2006 ircd-ratbox development team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1.Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * 2.Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 3.The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 */
#include "stdinc.h"
#include "ratbox_lib.h"
#include "client.h"
#include "s_conf.h"
#include "logger.h"
#include "match.h"
#include "bandbi.h"
#include "parse.h"
#include "channel.h"
#include "hostmask.h"
#include "hash.h"
#include "s_newconf.h"
#include "reject.h"
#include "send.h"
#include "ircd.h"
#include "msg.h"	/* XXX: MAXPARA */
#include "operhash.h"

static char bandb_add_letter[LAST_BANDB_TYPE] = {
	'K', 'D', 'X', 'R'
};

rb_dlink_list bandb_pending;

static rb_helper *bandb_helper;
static int start_bandb(void);

static void bandb_parse(rb_helper *);
static void bandb_restart_cb(rb_helper *);
static char *bandb_path;

void
init_bandb(void)
{
	if(start_bandb())
	{
		ilog(L_MAIN, "Unable to start bandb helper: %s", strerror(errno));
		exit(0);
	}
}


static int
start_bandb(void)
{
	char fullpath[PATH_MAX + 1];
#ifdef _WIN32
	const char *suffix = ".exe";
#else
	const char *suffix = "";
#endif

	rb_setenv("BANDB_DPATH", ConfigFileEntry.dpath, 1);
	if(bandb_path == NULL)
	{
		rb_snprintf(fullpath, sizeof(fullpath), "%s/bandb%s", BINPATH, suffix);

		if(access(fullpath, X_OK) == -1)
		{
			rb_snprintf(fullpath, sizeof(fullpath), "%s/bin/bandb%s",
				    ConfigFileEntry.dpath, suffix);

			if(access(fullpath, X_OK) == -1)
			{
				ilog(L_MAIN,
				     "Unable to execute bandb in %s or %s/bin",
				     BINPATH, ConfigFileEntry.dpath);
				return 0;
			}
		}
		bandb_path = rb_strdup(fullpath);
	}


	bandb_helper = rb_helper_start("bandb", bandb_path, bandb_parse, bandb_restart_cb);

	if(bandb_helper == NULL)
	{
		ilog(L_MAIN, "Unable to start bandb: %s", strerror(errno));
		sendto_realops_snomask(SNO_GENERAL, L_ALL, "Unable to start bandb: %s",
				     strerror(errno));
		return 1;
	}

	rb_helper_run(bandb_helper);
	return 0;
}

void
bandb_add(bandb_type type, struct Client *source_p, const char *mask1,
	  const char *mask2, const char *reason, const char *oper_reason, int perm)
{
	static char buf[BUFSIZE];

	rb_snprintf(buf, sizeof(buf), "%c %s ", bandb_add_letter[type], mask1);

	if(!EmptyString(mask2))
		rb_snprintf_append(buf, sizeof(buf), "%s ", mask2);

	rb_snprintf_append(buf, sizeof(buf), "%s %ld %d :%s",
			   get_oper_name(source_p), (long int)rb_current_time(), perm, reason);

	if(!EmptyString(oper_reason))
		rb_snprintf_append(buf, sizeof(buf), "|%s", oper_reason);

	rb_helper_write(bandb_helper, "%s", buf);
}

static char bandb_del_letter[LAST_BANDB_TYPE] = {
	'k', 'd', 'x', 'r'
};

void
bandb_del(bandb_type type, const char *mask1, const char *mask2)
{
	static char buf[BUFSIZE];

	buf[0] = '\0';

	rb_snprintf_append(buf, sizeof(buf), "%c %s", bandb_del_letter[type], mask1);

	if(!EmptyString(mask2))
		rb_snprintf_append(buf, sizeof(buf), " %s", mask2);

	rb_helper_write(bandb_helper, "%s", buf);
}

static void
bandb_handle_ban(char *parv[], int parc)
{
	struct ConfItem *aconf;
	char *p;
	int para = 1;

	aconf = make_conf();
	aconf->port = 0;

	if(parv[0][0] == 'K')
		aconf->user = rb_strdup(parv[para++]);

	aconf->host = rb_strdup(parv[para++]);
	aconf->info.oper = operhash_add(parv[para++]);

	switch (parv[0][0])
	{
	case 'K':
		aconf->status = CONF_KILL;
		break;

	case 'D':
		aconf->status = CONF_DLINE;
		break;

	case 'X':
		aconf->status = CONF_XLINE;
		break;

	case 'R':
		if(IsChannelName(aconf->host))
			aconf->status = CONF_RESV_CHANNEL;
		else
			aconf->status = CONF_RESV_NICK;

		break;
	}

	if((p = strchr(parv[para], '|')))
	{
		*p++ = '\0';
		aconf->spasswd = rb_strdup(p);
	}

	aconf->passwd = rb_strdup(parv[para]);

	rb_dlinkAddAlloc(aconf, &bandb_pending);
}

static int
bandb_check_kline(struct ConfItem *aconf)
{
	struct rb_sockaddr_storage daddr;
	struct ConfItem *kconf = NULL;
	int aftype;
	const char *p;

	aftype = parse_netmask(aconf->host, (struct sockaddr *)&daddr, NULL);

	if(aftype != HM_HOST)
	{
#ifdef RB_IPV6
		if(aftype == HM_IPV6)
			aftype = AF_INET6;
		else
#endif
			aftype = AF_INET;

		kconf = find_conf_by_address(aconf->host, NULL, NULL, (struct sockaddr *)&daddr,
					     CONF_KILL, aftype, aconf->user, NULL);
	}
	else
		kconf = find_conf_by_address(aconf->host, NULL, NULL, NULL, CONF_KILL, 0, aconf->user, NULL);

	if(kconf && ((kconf->flags & CONF_FLAGS_TEMPORARY) == 0))
		return 0;

	for(p = aconf->user; *p; p++)
	{
		if(!IsUserChar(*p) && !IsKWildChar(*p))
			return 0;
	}

	for(p = aconf->host; *p; p++)
	{
		if(!IsHostChar(*p) && !IsKWildChar(*p))
			return 0;
	}

	return 1;
}

static int
bandb_check_dline(struct ConfItem *aconf)
{
	struct rb_sockaddr_storage daddr;
/* 	struct ConfItem *dconf; */
	int bits;

	if(!parse_netmask(aconf->host, (struct sockaddr *)&daddr, &bits))
		return 0;

	return 1;
}

static int
bandb_check_xline(struct ConfItem *aconf)
{
	struct ConfItem *xconf;
	/* XXX perhaps convert spaces to \s? -- jilles */

	xconf = find_xline_mask(aconf->host);
	if(xconf != NULL && !(xconf->flags & CONF_FLAGS_TEMPORARY))
		return 0;

	return 1;
}

static int
bandb_check_resv_channel(struct ConfItem *aconf)
{
	const char *p;

	if(hash_find_resv(aconf->host) || strlen(aconf->host) > CHANNELLEN)
		return 0;

	for(p = aconf->host; *p; p++)
	{
		if(!IsChanChar(*p))
			return 0;
	}

	return 1;
}

static int
bandb_check_resv_nick(struct ConfItem *aconf)
{
	if(!clean_resv_nick(aconf->host))
		return 0;

	if(find_nick_resv(aconf->host))
		return 0;

	return 1;
}

static void
bandb_handle_clear(void)
{
	rb_dlink_node *ptr, *next_ptr;

	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, bandb_pending.head)
	{
		free_conf(ptr->data);
		rb_dlinkDestroy(ptr, &bandb_pending);
	}
}

static void
bandb_handle_finish(void)
{
	struct ConfItem *aconf;
	rb_dlink_node *ptr, *next_ptr;

	clear_out_address_conf_bans();
	clear_s_newconf_bans();

	RB_DLINK_FOREACH_SAFE(ptr, next_ptr, bandb_pending.head)
	{
		aconf = ptr->data;

		rb_dlinkDestroy(ptr, &bandb_pending);

		switch (aconf->status)
		{
		case CONF_KILL:
			if(bandb_check_kline(aconf))
				add_conf_by_address(aconf->host, CONF_KILL, aconf->user, NULL, aconf);
			else
				free_conf(aconf);

			break;

		case CONF_DLINE:
			if(bandb_check_dline(aconf))
				add_conf_by_address(aconf->host, CONF_DLINE, aconf->user, NULL, aconf);
			else
				free_conf(aconf);

			break;

		case CONF_XLINE:
			if(bandb_check_xline(aconf))
				rb_dlinkAddAlloc(aconf, &xline_conf_list);
			else
				free_conf(aconf);

			break;

		case CONF_RESV_CHANNEL:
			if(bandb_check_resv_channel(aconf))
				add_to_resv_hash(aconf->host, aconf);
			else
				free_conf(aconf);

			break;

		case CONF_RESV_NICK:
			if(bandb_check_resv_nick(aconf))
				rb_dlinkAddAlloc(aconf, &resv_conf_list);
			else
				free_conf(aconf);

			break;
		}
	}

	check_banned_lines();
}

static void
bandb_handle_failure(rb_helper *helper, char **parv, int parc)
{
	if(server_state_foreground)
		fprintf(stderr, "bandb - bandb failure: %s\n", parv[1]);
	
	ilog(L_MAIN, "bandb - bandb failure: %s", parv[1]);
	sendto_realops_snomask(SNO_GENERAL, L_ALL, "bandb - bandb failure: %s", parv[1]);
	exit(1);
}

static void
bandb_parse(rb_helper *helper)
{
	static char buf[READBUF_SIZE];
	char *parv[MAXPARA + 1];
	int len, parc;

	while((len = rb_helper_read(helper, buf, sizeof(buf))))
	{
		parc = rb_string_to_array(buf, parv, MAXPARA);

		if(parc < 1)
			continue;

		switch (parv[0][0])
		{
		case '!':
			bandb_handle_failure(helper, parv, parc);
			break;
		case 'K':
		case 'D':
		case 'X':
		case 'R':
			bandb_handle_ban(parv, parc);
			break;

		case 'C':
			bandb_handle_clear();
		case 'F':
			bandb_handle_finish();
			break;
		}
	}
}

void
bandb_rehash_bans(void)
{
	if(bandb_helper != NULL)
		rb_helper_write(bandb_helper, "L");
}

static void
bandb_restart_cb(rb_helper *helper)
{
	ilog(L_MAIN, "bandb - bandb_restart_cb called, bandb helper died?");
	sendto_realops_snomask(SNO_GENERAL, L_ALL,
			     "bandb - bandb_restart_cb called, bandb helper died?");
	if(helper != NULL)
	{
		rb_helper_close(helper);
		bandb_helper = NULL;
	}
	start_bandb();
	return;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.