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
|
/*
* Copyright (c) 2005-2010 Thierry FOURNIER
* $Id: maclist.c 696 2008-03-31 18:44:59Z $
*
*/
#include "config.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "capture.h"
#include "arpalert.h"
#include "maclist.h"
#include "data.h"
#include "loadconfig.h"
#include "log.h"
#include "func_time.h"
#include "func_str.h"
#if (__sun)
# define INADDR_NONE ((in_addr_t)(-1))
#endif
#define BUFFER_SIZE 1024
#define MAX_ARGS 20
extern int errno;
#define TEST 1
#define LOAD 2
#define RELOAD 3
/* parse maclist
* @file_name: file to parse
* @level: ALLOW : file contain allow macc addr (white list)
* DENY : file contain deny mac addr (black list)
* APPEND : load leases file
* @mode: TEST : test file and return status
* LOAD : load file
* RELOAD : load file and replace mac in memory
*
* @return: 0 if success
* -1 if error
*/
int maclist_file(char *file_name, int level, int mode){
char buf[BUFFER_SIZE];
FILE *file;
char *args[MAX_ARGS];
int ligne = 0;
int arg, blank, i;
char *parse;
struct ether_addr mac;
struct in_addr ip;
U_INT32_T bitfield;
struct capt *dev;
struct timeval discover;
struct timeval comp;
// open file
file = fopen(file_name, "r");
if(file == NULL){
// the leases file is not found ...
// maybe does not exist. is not an error
// just log
if(errno == ENOENT && level == APPEND){
logmsg(LOG_NOTICE, "Leases file (%s) not found",
file_name);
return 0;
}
logmsg(LOG_ERR, "[%s %d] fopen[%d]: %s (%s)",
__FILE__, __LINE__,
errno, strerror(errno),
file_name);
return -1;
}
buf[0] = 0;
// for each line ..
while(feof(file) == 0){
bzero(buf, sizeof(char) * BUFFER_SIZE);
fgets(buf, BUFFER_SIZE, file);
ligne ++;
// parse and return arguments list
arg = 0;
bzero(args, sizeof(char *) * MAX_ARGS);
blank = 1;
parse = buf;
while(*parse != 0){
// caractere nul:
if(*parse == ' ' || *parse == '\t'){
*parse = 0;
blank = 1;
parse ++;
}
// last caracter => quit
else if( *parse == '#' || *parse == '\n' || *parse == '\r'){
*parse = 0;
break;
}
// other caracters
else {
if(blank == 1){
args[arg] = parse;
arg ++;
// exceed args hard limit
if(arg == MAX_ARGS) {
logmsg(LOG_ERR,
"file: \"%s\", line %d: "
"exceed hard args limit (%d)",
file_name, ligne, MAX_ARGS);
return -1;
}
blank = 0;
}
parse++;
}
}
// proceed args
if(arg == 0) continue;
// sanity check
if(arg < 3){
logmsg(LOG_ERR,
"file: \"%s\", line %d: insufficient arguments",
file_name, ligne);
return -1;
}
// first arg: mac addr
if(str_to_mac(args[0], &mac) == -1){
logmsg(LOG_ERR,
"file: \"%s\", line %d: mac address error",
file_name, ligne);
return -1;
}
// convert string ip to numeric IP
ip.s_addr = inet_addr(args[1]);
if(ip.s_addr == INADDR_NONE){
logmsg(LOG_ERR,
"file: \"%s\", line %d: ip address error: %s",
file_name, ligne, args[1]);
return -1;
}
// pointer to interface
dev = cap_get_interface(args[2]);
if(dev == NULL){
logmsg(LOG_ERR,
"file: \"%s\", line %d: device \"%s\" not found/used",
file_name, ligne, args[2]);
return -1;
}
// if the loaded file are black list or white list
if(level == ALLOW || level == DENY){
// check flags
bitfield = 0;
i = 3;
while(i < arg){
/**/ if(strcmp(args[i], "ip_change") == 0){
SET_IP_CHANGE(bitfield);
}
else if(strcmp(args[i], "black_listed") == 0){
SET_BLACK_LISTED(bitfield);
}
else if(strcmp(args[i], "unauth_rq") == 0){
SET_UNAUTH_RQ(bitfield);
}
else if(strcmp(args[i], "rq_abus") == 0){
SET_RQ_ABUS(bitfield);
}
else if(strcmp(args[i], "mac_error") == 0){
SET_MAC_ERROR(bitfield);
}
else if(strcmp(args[i], "mac_change") == 0){
SET_MAC_CHANGE(bitfield);
}
else if(strcmp(args[i], "mac_expire") == 0){
SET_MAC_EXPIRE(bitfield);
}
else {
logmsg(LOG_ERR,
"file: \"%s\", line %d: flag \"%s\" not available",
file_name, ligne, args[i]);
return -1;
}
i++;
}
// add data
if(mode == LOAD){
data_add_field(&mac, level, ip, bitfield, dev);
}
else if(mode == RELOAD){
data_update_field(&mac, level, ip, bitfield, dev);
}
}
// if the loaded file are leases file
else if(level == APPEND){
// sanity check
if(arg < 5){
logmsg(LOG_ERR,
"file: \"%s\", line %d: insufficient arguments "
"for leases file",
file_name, ligne);
return -1;
}
// get discovering date of packet date
discover.tv_sec = atoi(args[3]);
discover.tv_usec = atoi(args[4]);
if(mode == LOAD){
// check if time is expired
comp.tv_sec = discover.tv_sec +
config[CF_TOOOLD].valeur.integer;
comp.tv_usec = discover.tv_usec;
if(time_comp(&comp, ¤t_t) == BIGEST){
// add data
data_add_time(&mac, level, ip, dev, &discover);
}
}
}
}
// close file
if(fclose(file) == EOF){
logmsg(LOG_ERR, "[%s %d] fclose[%d]: %s",
__FILE__, __LINE__, errno, strerror(errno));
exit(1);
}
// end
return 0;
}
void maclist_load(void){
int retval;
// load white list
if(config[CF_MACLIST].valeur.string != NULL &&
config[CF_MACLIST].valeur.string[0] != 0){
retval = maclist_file(config[CF_MACLIST].valeur.string,
ALLOW, LOAD);
if(retval == -1){
exit(1);
}
}
// load black list
if(config[CF_BLACKLST].valeur.string != NULL &&
config[CF_BLACKLST].valeur.string[0] != 0){
retval = maclist_file(config[CF_BLACKLST].valeur.string,
DENY, LOAD);
if(retval == -1){
exit(1);
}
}
// load leases
if(config[CF_LEASES].valeur.string != NULL &&
config[CF_LEASES].valeur.string[0] != 0){
retval = maclist_file(config[CF_LEASES].valeur.string,
APPEND, LOAD);
if(retval == -1){
exit(1);
}
}
}
void maclist_reload(void){
int retval;
#ifdef DEBUG
logmsg(LOG_DEBUG, "[%s %d %s] reload maclist",
__FILE__, __LINE__, __FUNCTION__);
#endif
// test white list
if(config[CF_MACLIST].valeur.string != NULL &&
config[CF_MACLIST].valeur.string[0] != 0){
retval = maclist_file(config[CF_MACLIST].valeur.string,
ALLOW, TEST);
if(retval == -1){
logmsg(LOG_ERR,
"errors in file \"%s\": not reload",
config[CF_MACLIST].valeur.string);
return;
}
}
// clear old status
data_reset_status();
// test black list
if(config[CF_BLACKLST].valeur.string != NULL &&
config[CF_BLACKLST].valeur.string[0] != 0){
retval = maclist_file(config[CF_BLACKLST].valeur.string,
DENY, TEST);
if(retval == -1){
logmsg(LOG_ERR,
"errors in file \"%s\": not reload",
config[CF_MACLIST].valeur.string);
return;
}
}
// reload white list
if(config[CF_MACLIST].valeur.string != NULL &&
config[CF_MACLIST].valeur.string[0] != 0){
retval = maclist_file(config[CF_MACLIST].valeur.string,
ALLOW, RELOAD);
if(retval == -1){
exit(1);
}
}
// reload black list
if(config[CF_BLACKLST].valeur.string != NULL &&
config[CF_BLACKLST].valeur.string[0] != 0){
retval = maclist_file(config[CF_BLACKLST].valeur.string,
DENY, RELOAD);
if(retval == -1){
exit(1);
}
}
}
|