ze-filter  (ze-filter-0.8.0-develop-180218)
zeAccess.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : Thu Jun 19 18:43:08 CEST 2014
12  *
13  * This program is free software - GPL v2.,
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  */
20 
21 
22 #include <ze-sys.h>
23 #include <ze-filter.h>
24 #include <zeAccess.h>
25 
26 
27 /*
28 * access database keys :
29 * AccessConnect:IP.AD.RE.SS
30 * AccessFrom:user@domain.com
31 * AccessTo:user@domain.com
32 * values :
33 * step:value,value:action(step:value,value:action)
34 * example :
35 * AccessConnect: to:*@mines-paristech.fr,*@ensmp.fr:OK; to:*:REJECT
36 *
37 */
38 
39 
40 static bool EmailMatch(char *, char *);
41 static bool IPv4AddrMatch(char *, char *);
42 
43 /* ****************************************************************************
44  * *
45  * *
46  ******************************************************************************/
47 int
48 AccessLookup(addr, from, to)
49  char *addr;
50  char *from;
51  char *to;
52 {
53  int access = ACCESS_OK;
54  char buf[1024];
55  char rBuf[256];
56  bool match = FALSE;
57 
58  memset(rBuf, 0, sizeof(rBuf));
59  if (to == NULL)
60  goto fin;
61 
62 chkfrom:
63  if (from == NULL)
64  goto chkaddr;
65 
66  if (PolicyLookupEmailAddr("AccessFrom", from, buf, sizeof (buf))) {
67  char *argvA[32], *argvB[32], *argvC[32];
68  int argcA, argcB, argcC;
69  int i, j, k;
70 
71  argcA = zeStr2Tokens(buf, 32, argvA, ";");
72  for (i = 0; i < argcA && !match; i++) {
73  argcB = zeStr2Tokens(argvA[i], 32, argvB, ":");
74  if (argcB == 2) {
75  argcC = zeStr2Tokens(argvB[0], 32, argvC, ",");
76  for (k = 0; k < argcC && !match; k++) {
77  if (EmailMatch(to, argvC[k])) {
78  strlcpy(rBuf, argvB[1], sizeof(rBuf));
79  match = TRUE;
80  }
81  }
82  }
83  }
84  }
85  if (match)
86  goto fin;
87 
88 chkaddr:
89  if (addr == NULL)
90  goto fin;
91 
92  if (PolicyLookupIPv4Addr("AccessConnect", addr, buf, sizeof (buf))) {
93  char *argvA[32], *argvB[32], *argvC[32];
94  int argcA, argcB, argcC;
95  int i, j, k;
96 
97  argcA = zeStr2Tokens(buf, 32, argvA, ";");
98  for (i = 0; i < argcA && !match; i++) {
99  argcB = zeStr2Tokens(argvA[i], 32, argvB, ":");
100  if (argcB == 2) {
101  argcC = zeStr2Tokens(argvB[0], 32, argvC, ",");
102  for (k = 0; k < argcC && !match; k++) {
103  if (EmailMatch(to, argvC[k])) {
104  strlcpy(rBuf, argvB[1], sizeof(rBuf));
105  match = TRUE;
106  }
107  }
108  }
109  }
110  }
111 
112 fin:
113 
114  if (match) {
115  printf("* Match %-9s : %s\n", match ? "found" : "not found", rBuf);
116  if (STRCASEEQUAL(rBuf, "OK"))
117  access = ACCESS_OK;
118  else if (STRCASEEQUAL(rBuf, "REJECT"))
119  access = ACCESS_REJECT;
120  else if (STRCASEEQUAL(rBuf, "TMPFAIL"))
121  access = ACCESS_TMPFAIL;
122  }
123 
124  return access;
125 }
126 
127 /* ****************************************************************************
128  * *
129  * *
130  ******************************************************************************/
131 bool
133 {
134 
135  return TRUE;
136 }
137 
138 bool
140 {
141 
142  return TRUE;
143 }
144 
145 bool
147 {
148 
149  return TRUE;
150 }
151 
152 bool
154 {
155 
156  return TRUE;
157 }
158 
159 bool
161 {
162 
163  return TRUE;
164 }
165 
166 /* ****************************************************************************
167  * *
168  * *
169  ******************************************************************************/
170 static bool
171 EmailMatch(email, target)
172  char *email;
173  char *target;
174 {
175  bool match = FALSE;
176  char *tEmail = NULL;
177  char *argv[3];
178  int argc;
179  char buf[1024];
180  int i;
181 
182  if (email == NULL || target == NULL)
183  return match;
184 
185  if (STRCASEEQUAL(email, target)) {
186  match = TRUE;
187  goto fin;
188  }
189 
190  tEmail = strdup(email);
191  if (tEmail == NULL) {
192  goto fin;
193  }
194  argc = zeStr2Tokens(tEmail, 3, argv, "@");
195  for (i = 0; i < argc; i++) {
196  if (STRCASEEQUAL(argv[i], target)) {
197  match = TRUE;
198  goto fin;
199  }
200  }
201  if (argc == 2) {
202  char *domKey, *userKey;
203  char *argvT[32];
204  int argcT;
205 
206  userKey = argv[0];
207  domKey = argv[1];
208 
209  snprintf(buf, sizeof (buf), "*@%s", domKey);
210  if (STRCASEEQUAL(buf, target)) {
211  match = TRUE;
212  goto fin;
213  }
214  snprintf(buf, sizeof (buf), "%s@*", userKey);
215  if (STRCASEEQUAL(buf, target)) {
216  match = TRUE;
217  goto fin;
218  }
219 
220  argcT = zeStr2Tokens(domKey, 32, argvT, ".");
221  for (i = 0; i < argcT; i++) {
222  char *lKey = NULL;
223 
224  argvT[i] = "*";
225  lKey = zeStrJoin(".", argcT - i, &argvT[i]);
226 
227  snprintf(buf, sizeof (buf), "%s@%s", userKey, lKey);
228  if (STRCASEEQUAL(buf, target)) {
229  match = TRUE;
230  FREE(lKey);
231  break;
232  }
233 
234  snprintf(buf, sizeof (buf), "%s@%s", "*", lKey);
235  if (STRCASEEQUAL(buf, target)) {
236  match = TRUE;
237  FREE(lKey);
238  break;
239  }
240 
241  if (STRCASEEQUAL(lKey, target)) {
242  match = TRUE;
243  FREE(lKey);
244  break;
245  }
246 
247  FREE(lKey);
248  }
249  if (match)
250  goto fin;
251  }
252 
253  if (STRCASEEQUAL("*", target))
254  match = TRUE;
255 
256 fin:
257  FREE(tEmail);
258 
259  return match;
260 }
261 
262 
263 /* ****************************************************************************
264  * *
265  * *
266  ******************************************************************************/
267 static bool
268 IPv4AddrMatch(a, b)
269  char *a;
270  char *b;
271 {
272  return FALSE;
273 }
bool AccessLookupConnect()
Definition: zeAccess.c:132
#define ACCESS_TMPFAIL
Definition: zeAccess.h:29
#define ACCESS_OK
Definition: zeAccess.h:28
char * zeStrJoin(char *, int, char **)
Definition: zeStrings.c:224
bool PolicyLookupEmailAddr(char *prefix, char *key, char *buf, size_t size)
Definition: zePolicy.c:230
#define FREE(x)
Definition: macros.h:37
#define FALSE
Definition: macros.h:160
#define strlcpy
Definition: zeString.h:32
int zeStr2Tokens(char *, int, char **, char *)
Definition: zeStrings.c:610
bool PolicyLookupIPv4Addr(char *prefix, char *key, char *buf, size_t size)
Definition: zePolicy.c:187
int AccessLookup(char *addr, char *from, char *to)
Definition: zeAccess.c:48
#define TRUE
Definition: macros.h:157
bool AccessLookupRcpt()
Definition: zeAccess.c:146
#define ACCESS_REJECT
Definition: zeAccess.h:30
bool AccessLookupFrom()
Definition: zeAccess.c:139
#define STRCASEEQUAL(a, b)
Definition: macros.h:72
bool AccessLookupEom()
Definition: zeAccess.c:160
bool AccessLookupData()
Definition: zeAccess.c:153