aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: aa4583d06cb2fd22b3275a09f4655549a88e3537 (plain)
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
/* This file is part of IPACCT
   Copyright (c) Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* $Id: main.c,v 1.1.1.1 2001/05/31 13:40:56 gray Exp $ */
#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pcap.h>
#include <pcap-int.h>
#include "ipacct.h"

#ifndef RETSIGTYPE
# define RETSIGTYPE void
#endif

void usage();
RETSIGTYPE sig_hup();
RETSIGTYPE sig_usr1();
RETSIGTYPE sig_usr2();
RETSIGTYPE sig_child();
static pcap_t  * init(char *device, char *cmd);

char *progname;
int dump_bytecode;        /* dump the packet-matched code and exit */
int promisc_off;          /* do not put the interface into promiscuous mode */
int optimize_expr;        /* optimize compiled expression */
int foreground;           /* remain in the foreground */
int fixed_clocks;         /* use fixed clocks */
int single_process = 0;   /* don't fork separate processes for accounting */
int verbose = 0;
char *profile = SYSCONFDIR "/" PROFILE;
char dump_name[MAX_PATH_LENGTH];
char *log_name;

char *device;
int snaplen = 96;
unsigned int localnet = 0, netmask = 0;
time_t start_time;
time_t slice_interval = TIME_TO_WAIT;
static pcap_t *pcap;
static char pidfile[MAX_PATH_LENGTH];

int
main(argc, argv)
	int             argc;
	char          **argv;
{
	int c, type;
	char *infile = NULL;
	char *expr = NULL;
	pcap_handler callback;
	time_t ttw = 0;
	pid_t pid;
	int syntax_check = 0;    /* check the config file and exit */
	
	progname = argv[0];

	while ((c = getopt(argc, argv, "bc:fFhi:l:OpsSt:vx")) != EOF) {
		switch (c) {
		case 'b':
			dump_bytecode++;
			break;
		case 'c':
			profile = optarg;
			break;
		case 'f':
			foreground++;
			break;
		case 'F':
			infile = optarg;
			break;
		case 'h':
			usage();
			return 0;
		case 'i':
			device = optarg;
			break;
		case 'l':
			log_name = optarg;
			break;
		case 'O':
			optimize_expr++;
			break;
		case 'p':
			promisc_off++;
			break;
		case 's':
			single_process++;
			break;
		case 'S':
			syntax_check++;
			break;
		case 't':
			ttw = atoi(optarg);
			if (ttw <= 0) 
				die(1, "invalid timeslice value (%d)", ttw);
			break;
		case 'v':
			verbose++;
			break;
		case 'x':
			fixed_clocks++;
			break;
		default:
			exit(0);
		}
	}

	read_profile(profile);
	if (syntax_check)
		return 0;
	alloc_snap_buffer();

	if (infile) {
		if (optind < argc) 
			die(1, "excess arguments after %s", argv[optind]);
		expr = read_expr(infile);
	} else if (optind < argc) 
		expr = copy_argv(argc, argv, optind);

	
	pcap = init(device, expr);
	if (!(callback = lookup_pcap_callback(type = pcap_datalink(pcap))))
		die(1, "unknown data link type 0x%x", type);

	if (pcap->fd < 0) 
		die(1, "can't listen to %d", pcap->fd);

	start_time = time(NULL);
	if (ttw)
		slice_interval = ttw;

	
	
	if (!foreground) {
		int i;
		pid_t pid = fork();
		if (pid < 0) 
			die(1, "cannot fork: %s", strerror(errno));
		if (pid > 0) {
			/* Parent branch */
			FILE *fp;
			/* write pid file */
			if ((fp = fopen(pidfile, "w")) != NULL) {
				fprintf(fp, "%d\n", pid);
				fclose(fp);
			} else {
				error("can't write pid file %s: %s",
				      pidfile, strerror(errno));
			}
			exit(0);
		}
		/* Child branch */
		chdir("/");
		for (i = 0; i < 3; i++)
			close(i);
		setsid();
		init_syslog();
	}

	run(pcap, callback);

	cleanup();
	return 0;
}

pcap_t  *
init(device, cmdbuf)
	char *device;
	char *cmdbuf;
{
	char buf[PCAP_ERRBUF_SIZE];
	struct bpf_program bpfcode;
	pcap_t *pd;
	pid_t pid;
	
	if (!device)
		if (!(device = pcap_lookupdev(buf)))
			die(1, buf);

	sprintf(pidfile, "/var/run/ipacct-%s.pid", device);
	if (!foreground && (pid = get_pid_from_file(pidfile)) > 0) 
		die(1, "the ipacct daemon seems to be running under pid %d.\n"
		    "If it is not, remove `%s' file and start ipacct again",
		    pid, pidfile);
	
	
	if (pd = pcap_open_live(device, snaplen, !promisc_off, 1000, buf)) {
		if ((pcap_lookupnet(device,
				    (u_int *) &localnet, (u_int *) & netmask,
				    buf)) < 0)
			die(1, buf);
	} else
		die(1, buf);
	
	setuid(getuid());

	bzero((char *) &bpfcode, sizeof(bpfcode));

	if (pcap_compile(pd, &bpfcode, cmdbuf, optimize_expr, netmask) < 0)
		die(1, pcap_geterr(pd));

	if (dump_bytecode) {
		bpf_dump(&bpfcode, dump_bytecode);
		exit(0);
	}
	if (pcap_setfilter(pd, &bpfcode) < 0)
		die(1, pcap_geterr(pd));

	signal(SIGPIPE, SIG_IGN);
	signal(SIGHUP, sig_hup);
        signal(SIGINT, (RETSIGTYPE (*)())cleanup);
	signal(SIGTERM, (RETSIGTYPE (*)())cleanup);
	signal(SIGUSR1, sig_usr1);
	signal(SIGUSR2, sig_usr2);
	signal(SIGCHLD, sig_child);

/*	cons_init();*/
	return pd;
}


int
open_process()
{
	pid_t pid;

	if (single_process)
		return 0;

	pid = fork();
	if (pid > 0) 
		return 1;
	else if (pid < 0) {
		error("fork: %s", strerror(errno));
		return -1;
	}
	
	signal(SIGHUP, SIG_IGN);
	signal(SIGUSR1, SIG_IGN);
	signal(SIGUSR2, SIG_IGN);
	signal(SIGCHLD, SIG_IGN);
	return 0;
}

int
close_process()
{
	if (!single_process)
		exit(0);
}

void
re_read_profile(name)
	char *name;
{
	addr_cleanup();
	read_profile(name);
}

void
cleanup()
{
	pcap_close(pcap);
	if (!foreground)
		unlink(pidfile);
	exit(0);
}

RETSIGTYPE
sig_hup()
{
	syslog(LOG_INFO, "reconfiguring");
	re_read_profile(profile);
	signal(SIGHUP, sig_hup);
}

RETSIGTYPE
sig_usr1(sig)
	int sig;
{
	dump_table(time(NULL));
	signal(SIGUSR1, sig_usr1);
}

RETSIGTYPE
sig_usr2()
{
}

RETSIGTYPE
sig_child(sig)
	int sig;
{
	int status;
	while (waitpid((pid_t)-1, &status, WNOHANG) > 0)
		;
	signal(sig, sig_child);
}

static char usage_str[] = "\
usage: ipacct [-c config][-fF][-i iface][-l log][-Ops][-t time][-x]\n\
       ipacct {-b|-h|-S}\n\
";
     
void
usage()
{
	printf("%s", usage_str);
}

Return to:

Send suggestions and report system problems to the System administrator.