File: alerte.c

package info (click to toggle)
arpalert 2.0.12-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,508 kB
  • sloc: ansic: 4,452; sh: 517; makefile: 116; perl: 35
file content (315 lines) | stat: -rw-r--r-- 6,831 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (c) 2005-2010 Thierry FOURNIER
 * $Id: alerte.c 690 2008-03-31 18:36:43Z  $
 *
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/time.h>

#include "arpalert.h"
#include "alerte.h"
#include "log.h"
#include "loadconfig.h"
#include "serveur.h"
#include "func_time.h"

// alert levels
const char *alert[] = {
	 "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",  "8",  "9",
	"10", "11", "12", "13", "14", "15", "16", "17", "18", "19"
};

char cmd_exec[2048];

extern int errno;

struct t_pid {
	int pid;
	struct timeval time;
	struct t_pid *next;
	struct t_pid *prev;
};

// used for allocate pid structur memory
struct t_pid *pid_alloc;

// unused base
struct t_pid unused_pid;

// used base
struct t_pid used_pid;

// pid list initialization 
void alerte_init(void){
	int counter;
	struct t_pid *assign;

	// init used pid chain
	used_pid.next = &used_pid;
	used_pid.prev = &used_pid;

	// if the script is not specified, quit function
	if(config[CF_ACTION].valeur.string == NULL ||
	   config[CF_ACTION].valeur.string[0] == 0){
		return;
	}

	// memory allocation for pid
	pid_alloc = (struct t_pid *) malloc(sizeof(struct t_pid) *
	            config[CF_MAXTH].valeur.integer);
	if(pid_alloc == NULL){
		logmsg(LOG_ERR, "[%s %i] malloc[%d]: %s",
		       __FILE__, __LINE__, errno, strerror(errno));
		exit(1);
	}

	// chain all pid in unused base
	assign = &unused_pid;
	counter = 0;
	while(counter < config[CF_MAXTH].valeur.integer){
		assign->next = &pid_alloc[counter];
		assign = assign->next;
		counter++;
	}
	assign->next = NULL;
}

// add a pid to list 
void addpid(int pid){
	struct t_pid *assign;

	// check if have a free process memory
	if(unused_pid.next == NULL){
		logmsg(LOG_ERR, "[%s %d] Process limit exceeded",
		       __FILE__, __LINE__);
		exit(1);
	}

	// set values
	assign = unused_pid.next;
	assign->pid = pid;
	assign->time.tv_sec = current_t.tv_sec +
	                      config[CF_TIMEOUT].valeur.integer;
	assign->time.tv_usec = current_t.tv_usec;

	// delete from the unused list
	unused_pid.next = unused_pid.next->next;
	
	// add at the end of chain
	assign->next = &used_pid;
	assign->prev = used_pid.prev;
	used_pid.prev->next = assign;
	used_pid.prev = assign;

	#ifdef DEBUG
	logmsg(LOG_DEBUG,
	       "[%s %i %s] Add pid %i",
	       __FILE__, __LINE__, __FUNCTION__,
	       assign->pid);
	#endif
}

// delete pid
void delpid(int pid){
	struct t_pid *assign;

	// if no current recorded pid
	if(used_pid.next == &used_pid) {
		return;
	}

	// find pid in pid chain
	assign = &used_pid;
	while(assign->pid != pid) {
		if(assign->next == &used_pid) {
			return;
		}
		assign = assign->next;
	}

	// delete pid from used list
	assign->next->prev = assign->prev;
	assign->prev->next = assign->next;

	// add pid to unused list
	assign->next = unused_pid.next;
	unused_pid.next = assign;
}

void alerte_kill_pid(void){
	int pid;

	#ifdef DEBUG
	logmsg(LOG_DEBUG,
	       "[%s %i %s] starting",
	       __FILE__, __LINE__, __FUNCTION__); 
	#endif

	while(TRUE){
		pid = waitpid(0, NULL, WNOHANG);
		
		// exit if no more child ended
		if(pid == 0 || ( pid == -1 && errno == ECHILD ) ){
			break;
		}
		
		// check error
		if(pid == -1 && errno != ECHILD){
			logmsg(LOG_ERR, "[%s %d] waitpid[%d]: %s",
				__FILE__, __LINE__, errno, strerror(errno));
			break;
		}
	
		#ifdef DEBUG
		logmsg(LOG_DEBUG,
		       "[%s %i %s] pid [%i] ended",
		       __FILE__, __LINE__, __FUNCTION__, pid); 
		#endif
		delpid(pid);
	}
}

// check validity of pids
void alerte_check(void){
	int return_code;
	int status;
	struct t_pid *check;
	struct t_pid *temp_check;

	#ifdef DEBUG
	logmsg(LOG_DEBUG,
	       "[%s %d %s] start cleanning processes",
	       __FILE__, __LINE__, __FUNCTION__);
	#endif

	// if no current recorded pid
	if(used_pid.next == &used_pid){
		#ifdef DEBUG
		logmsg(LOG_DEBUG, "[%s %i %s] no pid in pid list",
		       __FILE__, __LINE__, __FUNCTION__);
		#endif
		return;
	}

	// check all process
	check = used_pid.next;
	while(check != &used_pid){

		// record next occurance (the actual pointer maybe deleted)
		temp_check = check->next;
	
		// look if process's running
		return_code = waitpid(check->pid, &status, WNOHANG);

		// if time exceed
		if(return_code == 0 &&
		   time_comp(&current_t, &(check->time)) == BIGEST){
			logmsg(LOG_ERR, "kill pid %i: running time exceeded",
			       check->pid);

			// kill it
			if(kill(check->pid, 9) < 0){
				logmsg(LOG_ERR, "[%s %i] kill[%d]: %s",
				       __FILE__, __LINE__, errno, strerror(errno));
			}
		} 

		// if the process is stopped
		// else if(return_code == -1){
		else {
			#ifdef DEBUG
			logmsg(LOG_DEBUG, "[%s %i %s] pid %i is ended, removing "
			       "from check list",
			       __FILE__, __LINE__, __FUNCTION__,
			       check->pid);
			#endif

			// delete pid from list
			delpid(check->pid);
		}
		
		check = temp_check;
	}
}

// send an alert
void alerte_script(char *mac, char *ip, int alert_level, char *parm_supp,
                   char *interface, char *vendor){
	int return_pid;
	int return_code;
		  
	// if the script is not specified, quit function
	if(config[CF_ACTION].valeur.string == NULL ||
	   config[CF_ACTION].valeur.string[0] == 0){
		return;
	}

	if(unused_pid.next == NULL){
		logmsg(LOG_ERR, "Exceed maximun process currently running");
		return;
	}

	#ifdef DEBUG	
	logmsg(LOG_DEBUG, "[%s %i %s] Launch alert script [%s]", 			
          __FILE__, __LINE__, __FUNCTION__,
	       config[CF_ACTION].valeur.string);
	#endif

	return_pid = fork();
	if(return_pid == -1){
		logmsg(LOG_ERR, "[%s %i] fork[%d]: %s",
		       __FILE__, __LINE__, errno, strerror(errno));
		exit(1);
	}
	if(return_pid > 0){
		addpid(return_pid);
		return;
	}

	if(config[CF_ALERT_VENDOR].valeur.integer == TRUE){
		return_code = execlp(config[CF_ACTION].valeur.string,
		                     config[CF_ACTION].valeur.string,
		                     mac, ip, parm_supp, interface,
		                     alert[alert_level], vendor,
		                     (char*)0);
	}

	else {
		return_code = execlp(config[CF_ACTION].valeur.string,
		                     config[CF_ACTION].valeur.string,
		                     mac, ip, parm_supp, interface,
		                     alert[alert_level], (char*)0);
	}
	if(return_code < 0){
		logmsg(LOG_ERR, "[%s %i] execlp[%d]: %s"
		       __FILE__, __LINE__, errno, strerror(errno));
		exit(1);
	}
	exit(0);
}

// return the next active timeout
void *alerte_next(struct timeval *tv){
	struct t_pid *check;

	// if no pid running return NULL
	if(used_pid.next == &used_pid){
		tv->tv_sec = -1;
		return NULL;
	}

	check = used_pid.next;
	tv->tv_sec = check->time.tv_sec;
	tv->tv_usec = check->time.tv_usec;
	return alerte_check;
}