File: greylist.c

package info (click to toggle)
mdk4 4.1%2Bgit20200420-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,140 kB
  • sloc: ansic: 13,634; makefile: 144
file content (127 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download | duplicates (3)
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "greylist.h"
#include "helpers.h"

struct greylist {
  struct ether_addr mac;
  struct greylist *next;
};

typedef enum
{
  BLACK_LIST,
  WHITE_LIST,

}list_type;

struct greylist *glist = NULL;
struct greylist *blist = NULL;
struct greylist *wlist = NULL;

char black = 0;
char white = 0;


struct greylist *add_to_greylist(struct ether_addr new, struct greylist *gl) {
  struct greylist *gnew = malloc(sizeof(struct greylist));

  gnew->mac = new;

  if (gl) {
    gnew->next = gl->next;
    gl->next = gnew;
  } else {
    gl = gnew;
    gnew->next = gnew;
  }

  return gl;
}

struct greylist *search_in_greylist(struct ether_addr mac, struct greylist *gl) {
  struct greylist *first;

  if (! gl) return NULL;

  first = gl;
  
  do {
    if (MAC_MATCHES(mac, gl->mac)) {
      return gl;
    }
    gl = gl->next;
  } while (gl != first);

  return NULL;
}

void load_greylist(list_type type, char *filename) {
  char *entry;

  if (filename) {

    entry = read_next_line(filename, 1);
    while(entry) 
    {
      if (!search_in_greylist(parse_mac(entry), glist)) //Only add new entries
      {
	      glist = add_to_greylist(parse_mac(entry), glist);
      }
      free(entry);
      entry = read_next_line(filename, 0);
    }

    if(type == BLACK_LIST)
    {
      blist = glist;
      black = 1;
    }
    else if(type == WHITE_LIST)
    {
      wlist = glist;
      white = 1;
    }
  }
}

void load_blacklist(char *filename)
{
  load_greylist(BLACK_LIST, filename);
}

void load_whitelist(char *filename)
{
  load_greylist(WHITE_LIST, filename);
}

char is_blacklisted(struct ether_addr mac) 
{
  struct greylist *entry; 

  if (black) 
  {
     entry = search_in_greylist(mac, blist);
    if (entry) 
      return 1;
  } 

  return 0;
}

char is_whitelisted(struct ether_addr mac) 
{
  struct greylist *entry; 

  if (white) 
  {
    entry = search_in_greylist(mac, wlist);

    if (entry) 
      return 1;
  } 

  return 0;
}