ze-filter  (ze-filter-0.8.0-develop-180218)
policy-test.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : janvier 2002
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 #include <ze-sys.h>
26 
27 #include <ze-filter.h>
28 
29 #include <ze-avclient.h>
30 
31 bool shall_notify_user(char *, bool);
32 
33 void
35  char *s;
36  int i;
37  int f;
38 {
39  char buf[256];
40 
41  memset(buf, 0, sizeof (buf));
42  strcpy(buf, s);
43  printf(" -> %s\n", buf);
44 
45  memset(buf, 0, sizeof (buf));
46  zeStrSet(buf, ' ', strlen(s) + 1);
47  buf[i] = buf[f] = '+';
48  printf(" -> %s\n", buf);
49 }
50 
51 
52 int
53 main(argc, argv)
54  int argc;
55  char **argv;
56 {
57  char out[256];
58  int res = 0;
59  char *in = "/tmp/Yaha.P";
60 
61  char question[2048];
62  char answer[2048];
63  char msg[2048];
64  int avres;
65 
66  extern int ze_logLevel;
67 
68  configure("ze-policy-test", conf_file, FALSE);
69 
70  memset(answer, 0, sizeof (answer));
71 
73  ze_logLevel = 10;
74 
75  {
76  char *ip;
77 
78  if (argc > 1)
79  ip = argv[1];
80  else
81  ip = "194.214.158.176";
82 
83  }
84 
85  if (1) {
86  char *prefix, *ip, *name, *from, *to;
87  bool flag;
88 
89  prefix = ip = name = from = to = NULL;
90 
91  prefix = STRBOOL(argc > 1, argv[1], "-");
92  ip = STRBOOL(argc > 2, argv[2], "-");
93  name = STRBOOL(argc > 3, argv[3], "-");
94  from = STRBOOL(argc > 4, argv[4], "-");
95  to = STRBOOL(argc > 5, argv[5], "-");
96 
97  ZE_MessageInfo(0, "Checking = %s %s %s %s", ip, name, from, to);
98 
99  if (db_policy_open(TRUE)) {
100  flag = check_policy_tuple(prefix, ip, name, NULL, from, to, FALSE);
101 
102  ZE_MessageInfo(0, "RESULT = %s", STRBOOL(flag, "YES", "NO"));
103 
104  db_policy_close();
105  } else
106  ZE_MessageInfo(0, "Error opening policy database !");
107 
108  return 0;
109 
110  }
111 #if 0
112  {
113  bool res = FALSE;
114 
115  char *ip, *from, *to, hostname[256];
116 
117  printf("ARGC = %d\n", argc);
118 
119  gethostname(hostname, sizeof (hostname));
120  /*
121  * strcpy(hostname, "minho");
122  */
123  ip = STRBOOL(argc > 1, argv[1], "1.2.3.4");
124  from = STRBOOL(argc > 2, argv[2], "martins@ensmp.fr");
125  to = STRBOOL(argc > 3, argv[3], "joe@ensmp.fr");
126 
127  res = grey_init(FALSE);
128  ZE_MessageInfo(0, "GREY_INIT : %s %s %s %s : %s",
129  STRNULL(ip, "IP"),
130  STRNULL(from, "FROM"),
131  STRNULL(to, "TO"),
132  STRNULL(hostname, "HOSTNAME"), STRBOOL(res, "TRUE",
133  "FALSE"));
134 
135  res = grey_check(ip, from, to, hostname);
136 
137  ZE_MessageInfo(0, "GREY_CHECK : %s %s %s : %s",
138  STRNULL(ip, "IP"),
139  STRNULL(from, "FROM"),
140  STRNULL(to, "TO"), STRBOOL(res, "TRUE", "FALSE"));
141 
142  ZE_MessageInfo(0, "TIME %ld", time(NULL));
143  grey_close();
144 
145  return 0;
146  }
147 #endif
148 
149  if (db_policy_open(TRUE)) {
150  long pi, pf;
151  char *s, *expr;
152  bool found;
153 
154  char *prefix;
155  char *key;
156 
157  char buf[256];
158 
159  if (argc > 1)
160  prefix = argv[1];
161 
162  if (argc > 2)
163  key = argv[2];
164 
165 #if 1
166  prefix = STRNULL(prefix, "");
167  key = STRNULL(key, "");
168 #else
169  prefix = "NetClass";
170  key = "194.214.158.200";
171 #endif
172  memset(buf, 0, sizeof (buf));
173  check_policy(prefix, key, buf, sizeof (buf), TRUE);
174  printf("POLICY : %s(%s) = %s\n", prefix, key, buf);
175  printf("\n");
176  }
177 
178  {
179  bool ok;
180  char *s;
181 
182  s = "joe@ensmp.fr";
183  ok = shall_notify_user(s, FALSE);
184  printf(" -> %-30s %s\n", s, STRBOOL(ok, "TRUE", "FALSE"));
185 
186  s = "martins@ensmp.fr";
187  ok = shall_notify_user(s, FALSE);
188  printf(" -> %-30s %s\n", s, STRBOOL(ok, "TRUE", "FALSE"));
189 
190  s = "joe@ensmp.com";
191  ok = shall_notify_user(s, FALSE);
192  printf(" -> %-30s %s\n", s, STRBOOL(ok, "TRUE", "FALSE"));
193 
194  s = "martins@joe";
195  ok = shall_notify_user(s, FALSE);
196  printf(" -> %-30s %s\n", s, STRBOOL(ok, "TRUE", "FALSE"));
197 
198  s = "martins@fr.joe";
199  ok = shall_notify_user(s, FALSE);
200  printf(" -> %-30s %s\n", s, STRBOOL(ok, "TRUE", "FALSE"));
201 
202  s = "charles@free.fr";
203  ok = shall_notify_user(s, FALSE);
204  printf(" -> %-30s %s\n", s, STRBOOL(ok, "TRUE", "FALSE"));
205  }
206 }
207 
208 bool
209 shall_notify_user(user_addr, to)
210  char *user_addr;
211  bool to;
212 {
213  bool result = FALSE;
214 
215  char buf[256];
216 
217  memset(buf, 0, sizeof (buf));
218  if (check_policy("NotifyUser", user_addr, buf, sizeof (buf), FALSE)) {
219  if (strcasecmp(buf, "NO") == 0)
220  return FALSE;
221  if (strcasecmp(buf, "YES") == 0)
222  return TRUE;
223  if (strcasecmp(buf, "RECIPIENT") == 0)
224  return to;
225  if (strcasecmp(buf, "SENDER") == 0)
226  return !to;
227  if (strcasecmp(buf, "DAILY") == 0)
228  return FALSE;
229  }
230 
231  return result;
232 }
#define STRBOOL(x, t, f)
Definition: macros.h:87
bool check_policy(char *prefix, char *key, char *buf, size_t size, bool cdef)
Definition: ze-policy.c:119
void print_str_bounds(char *s, int i, int f)
Definition: policy-test.c:34
int ze_logLevel
Definition: zeSyslog.c:34
#define STRNULL(x, r)
Definition: macros.h:81
bool ok
Definition: ze-connopen.c:59
void zeLog_SetOutput(bool, bool)
Definition: zeSyslog.c:490
#define FALSE
Definition: macros.h:160
int grey_check(char *, char *, char *, char *, bool *, bool)
Definition: ze-grey.c:491
bool shall_notify_user(char *, bool)
Definition: policy-test.c:209
bool db_policy_close()
Definition: ze-dbpolicy.c:105
int main(int argc, char **argv)
Definition: policy-test.c:53
char * zeStrSet(char *, int, int)
Definition: zeStrings.c:330
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
bool grey_init(char *, bool, int)
Definition: ze-grey.c:1326
bool db_policy_open(bool)
Definition: ze-dbpolicy.c:44
#define TRUE
Definition: macros.h:157
void grey_close()
Definition: ze-grey.c:1381
char * conf_file
Definition: ze-cf.c:38
int configure(char *, char *, bool)
Definition: ze-cf.c:1203
bool check_policy_tuple(char *prefix, char *ip, char *name, char *netclass, char *from, char *to, bool result)
Definition: ze-policy.c:277
int msg[MAX_SCORE+2]
Definition: ze-stats.c:41