aclck.c
4.67 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2005,
2007 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include <mailutils/mailutils.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
struct sockaddr *target_sa;
int target_salen;
mu_acl_t acl;
struct sockaddr *
parse_address (int *psalen, char *str)
{
struct sockaddr_in in;
struct sockaddr *sa;
in.sin_family = AF_INET;
if (inet_aton (str, &in.sin_addr) == 0)
{
mu_error ("Invalid IPv4: %s", str);
exit (1);
}
in.sin_port = 0;
*psalen = sizeof (in);
sa = malloc (*psalen);
if (!sa)
{
mu_error ("%s", mu_strerror (errno));
exit (1);
}
memcpy (sa, &in, sizeof (in));
return sa;
}
void
read_rules (FILE *fp)
{
char buf[512];
int line = 0;
int argc = 0;
char **argv;
int rc;
rc = mu_acl_create (&acl);
if (rc)
{
mu_error ("cannot create acl: %s", mu_strerror (rc));
exit (1);
}
while (fgets (buf, sizeof buf, fp))
{
unsigned long netmask;
int salen;
struct sockaddr *sa;
mu_acl_action_t action;
void *data = NULL;
char *p;
int len = strlen (buf);
if (len == 0)
continue;
if (buf[len-1] != '\n')
{
mu_error ("%d: line too long", line);
continue;
}
buf[len-1] = 0;
line++;
if (buf[0] == '#')
continue;
if (argc)
mu_argcv_free (argc, argv);
mu_argcv_get (buf, " \t", "#", &argc, &argv);
if (argc < 2)
{
mu_error ("%d: invalid input", line);
continue;
}
p = strchr (argv[1], '/');
if (p)
{
char *q;
unsigned netlen;
*p++ = 0;
netlen = strtoul (p, &q, 10);
if (*q == 0)
{
if (netlen == 0)
netmask = 0;
else
{
netmask = 0xfffffffful >> (32 - netlen);
netmask <<= (32 - netlen);
netmask = htonl (netmask);
}
}
else if (*q == '.')
{
struct in_addr addr;
if (inet_aton (p, &addr) == 0)
{
mu_error ("%d: invalid netmask", line);
continue;
}
netmask = addr.s_addr;
}
else
{
mu_error ("%d: invalid netmask", line);
continue;
}
}
else
netmask = 0xfffffffful;
sa = parse_address (&salen, argv[1]);
/* accept addr
deny addr
log addr [rest ...]
exec addr [rest ...]
execif addr rest ....]
*/
if (mu_acl_string_to_action (argv[0], &action))
{
mu_error ("%d: invalid command", line);
continue;
}
switch (action)
{
case mu_acl_accept:
case mu_acl_deny:
break;
case mu_acl_log:
case mu_acl_exec:
case mu_acl_ifexec:
data = strdup (argv[2]);
}
rc = mu_acl_append (acl, action, data, sa, salen, netmask);
if (rc)
mu_error ("%d: cannot append acl entry: %s", line,
mu_strerror (rc));
}
}
int
main (int argc, char **argv)
{
int rc;
FILE *file = NULL;
mu_acl_result_t result;
mu_set_program_name (argv[0]);
while ((rc = getopt (argc, argv, "Dd:a:f:")) != EOF)
{
switch (rc)
{
case 'D':
mu_debug_line_info = 1;
break;
case 'd':
mu_global_debug_from_string (optarg, "command line");
break;
case 'a':
target_sa = parse_address (&target_salen, optarg);
break;
case 'f':
file = fopen (optarg, "r");
if (file == 0)
{
mu_error ("cannot open file %s: %s", optarg,
mu_strerror (errno));
exit (1);
}
break;
default:
exit (1);
}
}
argv += optind;
argc -= optind;
read_rules (file ? file : stdin);
rc = mu_acl_check_sockaddr (acl, target_sa, target_salen, &result);
if (rc)
{
mu_error ("mu_acl_check_sockaddr failed: %s", mu_strerror (rc));
exit (1);
}
switch (result)
{
case mu_acl_result_undefined:
puts ("undefined");
break;
case mu_acl_result_accept:
puts ("accept");
break;
case mu_acl_result_deny:
puts ("deny");
break;
}
exit (0);
}