| 1 |
--- /dev/null |
| 2 |
+++ b/README.access-list |
| 3 |
@@ -0,0 +1,52 @@ |
| 4 |
+This version of ifmail has been patched to support access lists in iftoss. |
| 5 |
+There are three access lists, namely packet header one, message header one |
| 6 |
+and origin one. They are specified using the following keywords in a main |
| 7 |
+ifmail configuration file: |
| 8 |
+ |
| 9 |
+packetacl <filename> |
| 10 |
+messageacl <filename> |
| 11 |
+originacl <filename> |
| 12 |
+ |
| 13 |
+The three files are of the same format: if a line has a hash ('#') at its |
| 14 |
+beginning then it is a comment line; otherwise a line specifies a rule and |
| 15 |
+must have four fields delimited by whitespace characters (spaces or tabs). |
| 16 |
+The first field specifies the action of the rule: "permit" or "deny". |
| 17 |
+Those words must be typed in lowercase. The second field is a pattern for |
| 18 |
+areatag. The third and the fourth ones are patterns for source and destination |
| 19 |
+addresses, accordingly. The patterns are in the style of Unix shell ones. |
| 20 |
+See sh(1), fnmatch(3) or fnmatch(5) for their description. No escaping with |
| 21 |
+a backslash ('\') is available. |
| 22 |
+ |
| 23 |
+The destination address pattern field is ignored in the origin access list |
| 24 |
+and may be anything, but not empty. |
| 25 |
+ |
| 26 |
+An empty line is considered a error. Use comments to beautify your access |
| 27 |
+lists. |
| 28 |
+ |
| 29 |
+If any of the three files is not readable or does not exist at all then |
| 30 |
+the according access list is considered empty. |
| 31 |
+ |
| 32 |
+Access list matching is performed in a such way that the first matching rule |
| 33 |
+is used. If no matching rules found then the default is to permit anything. |
| 34 |
+ |
| 35 |
+Example (for origin access list): |
| 36 |
+# |
| 37 |
+# Allow Alex Semenyaka and his points to post to PVT.EXCH areas |
| 38 |
+# as he is actually in Msk |
| 39 |
+# |
| 40 |
+permit PVT.EXCH.* 2:461/640 * |
| 41 |
+permit PVT.EXCH.* 2:461/640.* * |
| 42 |
+# |
| 43 |
+# Deny posts to PVT.EXCH from R46 (on the Moderator's request) |
| 44 |
+# |
| 45 |
+deny PVT.EXCH.* 2:46*/* * |
| 46 |
+# |
| 47 |
+# Make this jerk and his points read-only in the famous area FOO.BAR |
| 48 |
+# because he've got a bang [!] from the Moderator |
| 49 |
+# |
| 50 |
+deny FOO.BAR 2:5020/12345 * |
| 51 |
+deny FOO.BAR 2:5020/12345.* * |
| 52 |
+# |
| 53 |
+# This is the default action. Placed here for convenience |
| 54 |
+# |
| 55 |
+permit * * * |
| 56 |
--- /dev/null |
| 57 |
+++ b/ifgate/acl.c |
| 58 |
@@ -0,0 +1,146 @@ |
| 59 |
+#include <errno.h> |
| 60 |
+#include <fnmatch.h> |
| 61 |
+#include <stdio.h> |
| 62 |
+#include <stdlib.h> |
| 63 |
+#include <string.h> |
| 64 |
+ |
| 65 |
+#include "ftn.h" |
| 66 |
+#include "acl.h" |
| 67 |
+#include "lutil.h" |
| 68 |
+#include "xutil.h" |
| 69 |
+ |
| 70 |
+int acl_default_action = PERMIT; |
| 71 |
+ |
| 72 |
+void tidy_acl(aclp) |
| 73 |
+ acl *aclp; |
| 74 |
+{ |
| 75 |
+ acl *taclp; |
| 76 |
+ |
| 77 |
+ while (aclp != NULL) { |
| 78 |
+ if (aclp->etag != NULL) free(aclp->etag); |
| 79 |
+ if (aclp->from != NULL) free(aclp->from); |
| 80 |
+ if (aclp->to != NULL) free(aclp->to); |
| 81 |
+ taclp = aclp->next; |
| 82 |
+ free(aclp); |
| 83 |
+ aclp = taclp; |
| 84 |
+ } |
| 85 |
+} |
| 86 |
+ |
| 87 |
+acl *read_acl(filename) |
| 88 |
+ char *filename; |
| 89 |
+{ |
| 90 |
+ int action; |
| 91 |
+ int linecount; |
| 92 |
+ FILE *fp; |
| 93 |
+ acl *aclp, *taclp; |
| 94 |
+ char action_s[256], etag_s[256], from_s[256], to_s[256]; |
| 95 |
+ char buf[1024]; |
| 96 |
+ |
| 97 |
+ if (filename == NULL) return NULL; |
| 98 |
+ |
| 99 |
+ if ((fp = fopen(filename, "r")) == NULL) { |
| 100 |
+ if (errno != ENOENT) |
| 101 |
+ logerr("$cannot open access list \"%s\"", filename); |
| 102 |
+ return NULL; |
| 103 |
+ } |
| 104 |
+ |
| 105 |
+ linecount = 1; |
| 106 |
+ aclp = taclp = NULL; |
| 107 |
+ |
| 108 |
+ while (fgets(buf, sizeof(buf)-1, fp) != NULL) { |
| 109 |
+ if (buf[0] == '#') continue; |
| 110 |
+ |
| 111 |
+ if (sscanf(buf, "%s%s%s%s", |
| 112 |
+ action_s, etag_s, from_s, to_s) != 4) { |
| 113 |
+ logerr("error in access list \"%s\", line %d", |
| 114 |
+ filename, linecount); |
| 115 |
+ continue; |
| 116 |
+ } |
| 117 |
+ |
| 118 |
+ if (strcmp(action_s, "deny") == 0) |
| 119 |
+ action = DENY; |
| 120 |
+ else if (strcmp(action_s, "permit") == 0) |
| 121 |
+ action = PERMIT; |
| 122 |
+ else { |
| 123 |
+ logerr("bad action in access list \"%s\", line %d", |
| 124 |
+ filename, linecount); |
| 125 |
+ continue; |
| 126 |
+ } |
| 127 |
+ |
| 128 |
+ if (taclp == NULL) |
| 129 |
+ aclp = taclp = (acl *)xmalloc(sizeof(acl)); |
| 130 |
+ else { |
| 131 |
+ taclp->next = (acl *)xmalloc(sizeof(acl)); |
| 132 |
+ taclp = taclp->next; |
| 133 |
+ } |
| 134 |
+ |
| 135 |
+ taclp->action = action; |
| 136 |
+ taclp->etag = xstrcpy(etag_s); |
| 137 |
+ taclp->from = xstrcpy(from_s); |
| 138 |
+ taclp->to = xstrcpy(to_s); |
| 139 |
+ taclp->next = NULL; |
| 140 |
+ } |
| 141 |
+ |
| 142 |
+ fclose(fp); |
| 143 |
+ return aclp; |
| 144 |
+} |
| 145 |
+ |
| 146 |
+int match_acl(etag, from, to, aclp) |
| 147 |
+ char *etag; |
| 148 |
+ faddr *from; |
| 149 |
+ faddr *to; |
| 150 |
+ acl *aclp; |
| 151 |
+{ |
| 152 |
+ int action = acl_default_action; |
| 153 |
+ char *etag_s = NULL, *from_s = NULL, *to_s = NULL; |
| 154 |
+ |
| 155 |
+ if (etag != NULL) { |
| 156 |
+ etag_s = xstrcpy(etag); |
| 157 |
+ if (etag_s[strlen(etag)-1] == '\n') |
| 158 |
+ etag_s[strlen(etag)-1] = '\0'; |
| 159 |
+ } |
| 160 |
+ |
| 161 |
+ if (from != NULL) |
| 162 |
+ from_s = xstrcpy(ascfnode(from, 0x0f)); |
| 163 |
+ |
| 164 |
+ if (to != NULL) |
| 165 |
+ to_s = xstrcpy(ascfnode(to, 0x0f)); |
| 166 |
+ |
| 167 |
+ |
| 168 |
+ for (; aclp != NULL; aclp = aclp->next) |
| 169 |
+ if ((etag == NULL || |
| 170 |
+ fnmatch(aclp->etag, etag_s, FNM_NOESCAPE) == 0) && |
| 171 |
+ (from == NULL || |
| 172 |
+ fnmatch(aclp->from, from_s, FNM_NOESCAPE) == 0) && |
| 173 |
+ (to == NULL || |
| 174 |
+ fnmatch(aclp->to, to_s, FNM_NOESCAPE) == 0)) { |
| 175 |
+ action = aclp->action; |
| 176 |
+ break; |
| 177 |
+ } |
| 178 |
+ |
| 179 |
+ if (etag_s != NULL) free(etag_s); |
| 180 |
+ if (from_s != NULL) free(from_s); |
| 181 |
+ if (to_s != NULL) free(to_s); |
| 182 |
+ |
| 183 |
+ return action; |
| 184 |
+} |
| 185 |
+ |
| 186 |
+#ifdef DEBUG |
| 187 |
+ |
| 188 |
+int main(int argc, char **argv) |
| 189 |
+{ |
| 190 |
+ int i; |
| 191 |
+ acl *aclp; |
| 192 |
+ |
| 193 |
+ aclp = read_acl(argv[1]); |
| 194 |
+ |
| 195 |
+ /*for (i=0; i<1000000; i++) |
| 196 |
+ match_acl(argv[2], argv[3], argv[4], aclp); |
| 197 |
+ */ |
| 198 |
+ |
| 199 |
+ printf("%d\n", match_acl(argv[2], argv[3], argv[4], aclp)); |
| 200 |
+ |
| 201 |
+ return 0; |
| 202 |
+} |
| 203 |
+ |
| 204 |
+#endif |
| 205 |
--- /dev/null |
| 206 |
+++ b/ifgate/acl.h |
| 207 |
@@ -0,0 +1,19 @@ |
| 208 |
+#ifndef _ACL_H_ |
| 209 |
+#define _ACL_H_ |
| 210 |
+ |
| 211 |
+#define PERMIT 0 |
| 212 |
+#define DENY 1 |
| 213 |
+ |
| 214 |
+typedef struct _acl { |
| 215 |
+ int action; |
| 216 |
+ char *etag; |
| 217 |
+ char *from; |
| 218 |
+ char *to; |
| 219 |
+ struct _acl *next; |
| 220 |
+} acl; |
| 221 |
+ |
| 222 |
+void tidy_acl(acl *); |
| 223 |
+acl * read_acl(char *); |
| 224 |
+int match_acl(char *, faddr *, faddr *, acl *); |
| 225 |
+ |
| 226 |
+#endif |
| 227 |
--- a/ifgate/getmessage.c |
| 228 |
+++ b/ifgate/getmessage.c |
| 229 |
@@ -20,6 +20,7 @@ |
| 230 |
#include "config.h" |
| 231 |
#include "crc.h" |
| 232 |
#include "charset.h" |
| 233 |
+#include "acl.h" |
| 234 |
|
| 235 |
#define KWDCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ |
| 236 |
"abcdefghijklmnopqrstuvwxyz" \ |
| 237 |
@@ -30,6 +31,8 @@ extern char *rfcdate(time_t); |
| 238 |
|
| 239 |
int pgpsigned; |
| 240 |
|
| 241 |
+extern acl *packet_acl, *message_acl, *origin_acl; |
| 242 |
+ |
| 243 |
static time_t parsefdate(char *,void *); |
| 244 |
static time_t parsefdate(str,now) |
| 245 |
char *str; |
| 246 |
@@ -59,6 +62,7 @@ faddr *p_from,*p_to; |
| 247 |
rfcmsg *kmsg=NULL,**tmsg; |
| 248 |
int tmp,rc,maxrc=0; |
| 249 |
faddr f,t,*o; |
| 250 |
+ faddr m_from, m_to; |
| 251 |
int flags; |
| 252 |
int waskluge,badkludge; |
| 253 |
time_t mdate=0L; |
| 254 |
@@ -153,6 +157,10 @@ faddr *p_from,*p_to; |
| 255 |
debug(5,"message subj \"%s\"",S(subj)); |
| 256 |
debug(5,"message date \"%s\"",rfcdate(mdate)); |
| 257 |
|
| 258 |
+ /* We need only numeric fields */ |
| 259 |
+ m_from=f; |
| 260 |
+ m_to=t; |
| 261 |
+ |
| 262 |
tear_off=0L; |
| 263 |
orig_off=0L; |
| 264 |
via_off=0L; |
| 265 |
@@ -336,11 +344,37 @@ faddr *p_from,*p_to; |
| 266 |
if ((via_off) && (via_off < endmsg_off)) endmsg_off=via_off; |
| 267 |
debug(5,"end message offset %ld",(long)endmsg_off); |
| 268 |
|
| 269 |
+ |
| 270 |
+ if (kmsg && !strcmp(kmsg->key,"AREA")) |
| 271 |
+ { |
| 272 |
+ if (match_acl(kmsg->val, p_from, p_to, packet_acl) == DENY) |
| 273 |
+ { |
| 274 |
+ loginf("message from %s in area %s denied by packet header filter", |
| 275 |
+ ascfnode(p_from, 0xf), kmsg->val); |
| 276 |
+ goto skip; |
| 277 |
+ } |
| 278 |
+ if (match_acl(kmsg->val, &m_from, &m_to, message_acl) == DENY) |
| 279 |
+ { |
| 280 |
+ loginf("message from %s in area %s denied by message header filter", |
| 281 |
+ ascfnode(&m_from, 0xf), kmsg->val); |
| 282 |
+ goto skip; |
| 283 |
+ } |
| 284 |
+ |
| 285 |
+ if (match_acl(kmsg->val, &f, NULL, origin_acl) == DENY) |
| 286 |
+ { |
| 287 |
+ loginf("message from %s in area %s denied by origin filter", |
| 288 |
+ ascfnode(&f, 0xf), kmsg->val); |
| 289 |
+ goto skip; |
| 290 |
+ } |
| 291 |
+ |
| 292 |
+ } |
| 293 |
+ |
| 294 |
rewind(fp); |
| 295 |
rc=mkrfcmsg(&f,&t,subj,orig,mdate,flags,fp,endmsg_off,kmsg); |
| 296 |
if (rc) rc+=10; |
| 297 |
if (rc > maxrc) maxrc=rc; |
| 298 |
|
| 299 |
+skip: |
| 300 |
fclose(fp); |
| 301 |
tidyrfc(kmsg); |
| 302 |
if(f.name) free(f.name); f.name=NULL; |
| 303 |
--- a/ifgate/iftoss.c |
| 304 |
+++ b/ifgate/iftoss.c |
| 305 |
@@ -18,6 +18,7 @@ |
| 306 |
#include "ftn.h" |
| 307 |
#include "getheader.h" |
| 308 |
#include "trap.h" |
| 309 |
+#include "acl.h" |
| 310 |
#ifdef DIRTY_CHRS |
| 311 |
#include "charset.h" |
| 312 |
int dirtyoutcode; |
| 313 |
@@ -41,6 +42,9 @@ extern void readalias(char *); |
| 314 |
extern int exclose(FILE *); |
| 315 |
|
| 316 |
extern int num_echo,num_mail; |
| 317 |
+extern long paranoid; |
| 318 |
+ |
| 319 |
+acl *packet_acl, *message_acl, *origin_acl; |
| 320 |
|
| 321 |
int usetmp=1; /* to tell bgets that we do not use batch mode */ |
| 322 |
int notransports=0; |
| 323 |
@@ -113,23 +117,34 @@ char *argv[]; |
| 324 |
#endif |
| 325 |
if (aliasfile) readalias(aliasfile); |
| 326 |
|
| 327 |
+ packet_acl=read_acl(pktaclfile); |
| 328 |
+ message_acl=read_acl(msgaclfile); |
| 329 |
+ origin_acl=read_acl(orgaclfile); |
| 330 |
+ |
| 331 |
if (notransports) |
| 332 |
{ |
| 333 |
mkdir(FAKEDIR,0777); |
| 334 |
loginf("messages/newsbatches will go to %s",FAKEDIR); |
| 335 |
} |
| 336 |
|
| 337 |
-#ifdef PARANOID |
| 338 |
- if (((rc=getheader(&from,&to,stdin)) != 0) && |
| 339 |
- ((rc != 3) || (!relaxed))) |
| 340 |
-#else |
| 341 |
- if (((rc=getheader(&from,&to,stdin)) != 0) && |
| 342 |
- ((rc != 3) || (!relaxed)) && |
| 343 |
- (rc != 4)) |
| 344 |
-#endif |
| 345 |
- { |
| 346 |
- logerr("%s, aborting",(rc==3)?"packet not to this node": |
| 347 |
- (rc==4)?"bad password":"bad packet"); |
| 348 |
+ switch ((rc=getheader(&from,&to,stdin))) { |
| 349 |
+ case 0: |
| 350 |
+ break; |
| 351 |
+ case 2: |
| 352 |
+ logerr("bad packet, aborting"); |
| 353 |
+ exit(rc); |
| 354 |
+ case 3: |
| 355 |
+ if (relaxed) |
| 356 |
+ break; |
| 357 |
+ logerr("packet not to this node, aborting"); |
| 358 |
+ exit(rc); |
| 359 |
+ case 4: |
| 360 |
+ if (!paranoid) |
| 361 |
+ break; |
| 362 |
+ logerr("bad password, aborting"); |
| 363 |
+ exit(rc); |
| 364 |
+ default: |
| 365 |
+ logerr("can't happen: getheader returned %d", rc); |
| 366 |
exit(rc); |
| 367 |
} |
| 368 |
#ifdef AREAS_HACKING |
| 369 |
--- a/ifgate/Makefile |
| 370 |
+++ b/ifgate/Makefile |
| 371 |
@@ -18,7 +18,7 @@ OBJMAIL = version.o ifmail.o rfcmsg.o me |
| 372 |
charconv.o charconv_jp.o charconv_hz.o charconv_utf.o |
| 373 |
OBJTOSS = version.o iftoss.o areas.o \ |
| 374 |
getmessage.o mkrfcmsg.o rfcmsg.o batchrd.o \ |
| 375 |
- ifdbm.o backalias.o msgflags.o \ |
| 376 |
+ ifdbm.o backalias.o msgflags.o acl.o \ |
| 377 |
charconv.o charconv_jp.o charconv_hz.o charconv_utf.o |
| 378 |
OBJUNPACK = version.o ifunpack.o unpacker.o flock.o |
| 379 |
OBJPACK = version.o ifpack.o flock.o |
| 380 |
@@ -28,10 +28,10 @@ SRCS = ifmail.c rfcmsg.c message.c mkftn |
| 381 |
iftoss.c getmessage.c mkrfcmsg.c \ |
| 382 |
nlindex.c nodecheck.c \ |
| 383 |
ifunpack.c unpacker.c ifpack.c flock.c \ |
| 384 |
- backalias.c msgidbm.c attach.c ifstat.c lastmtime.c \ |
| 385 |
+ backalias.c msgidbm.c attach.c ifstat.c lastmtime.c acl.c \ |
| 386 |
body.c charconv.c charconv_jp.c charconv_hz.c charconv_utf.c |
| 387 |
HDRS = areas.h mkrfcmsg.h mkftnhdr.h nlindex.h nodecheck.h \ |
| 388 |
- charconv.h charconv_jp.h charconv_hz.h |
| 389 |
+ charconv.h charconv_jp.h charconv_hz.h acl.h |
| 390 |
OTHER = README Makefile testmail newsin pkt ifmail.8 iftoss.8 |
| 391 |
ALL = ifmail ifnews iftoss ifunpack ifpack ifstat |
| 392 |
|
| 393 |
--- a/iflib/config.h |
| 394 |
+++ b/iflib/config.h |
| 395 |
@@ -139,6 +139,10 @@ extern int defaultrfcchar; |
| 396 |
extern int defaultftnchar; |
| 397 |
extern int toftnchar; |
| 398 |
|
| 399 |
+extern char *pktaclfile; |
| 400 |
+extern char *msgaclfile; |
| 401 |
+extern char *orgaclfile; |
| 402 |
+ |
| 403 |
int readconfig(void); |
| 404 |
int confopt(int,char*); |
| 405 |
void confusage(char*); |