s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
mfrules.c
Go to the documentation of this file.
1#include "alloc.h"
2#include "stralloc.h"
3#include "open.h"
4#include "cdbread.h"
5#include "case.h"
6#include "mfrules.h"
7#include "str.h"
8#include "byte.h"
9#include "close.h"
10
11/* return -9: problems reading cdb */
12/* return -1: key matches; data not */
13/* return 0: no key */
14/* return 1: key matches without data */
15/* return 2: key and data match */
16
17stralloc key = {0};
18
19static struct cdb cdb;
20
21static int mffind(char *mf)
22{
23 char *x;
24 char *data;
25 unsigned int datalen;
26 int plus = 0;
27 int dlen;
28 int len;
29 int mflen;
30 int delta;
31
32 switch (cdb_find(&cdb,key.s,key.len)) {
33 case -1: return -9;
34 case 0: return 0;
35 }
36
37 datalen = cdb_datalen(&cdb);
38 data = alloc(datalen);
39 if (!data) return -9;
40 if (!datalen) return 1;
41 mflen = str_len(mf);
42
43 if (cdb_read(&cdb,data,datalen,cdb_datapos(&cdb)) == -1) {
44 alloc_free(data);
45 return -9;
46 }
47
48 x = data; dlen = datalen - 1; /* trailing separator */
49
50 while (dlen > 0) {
51 plus = byte_rchr(data,dlen,'+');
52 x = data + plus + 1;
53 len = dlen - plus;
54 delta = (mflen > len) ? mflen - len : 0;
55 if (!byte_diff(x,len,mf + delta)) { alloc_free(data); return 2; }
56 dlen = plus - 1;
57 }
58
59 alloc_free(data);
60 return -1;
61}
62
63int mfsearch(char *ip,char *host,char *info,char *mf)
64{
65 int r;
66
67 if (info) {
68 if (!stralloc_copys(&key,info)) return -9;
69 r = mffind(mf);
70 if (r < -1 || r > 0) return r;
71
72 if (!stralloc_cats(&key,"@")) return -9;
73 if (!stralloc_cats(&key,ip)) return -9;
74 r = mffind(mf);
75 if (r < -1 || r > 0) return r;
76
77 if (host) {
78 if (!stralloc_copys(&key,info)) return -9;
79 if (!stralloc_cats(&key,"@=")) return -9;
80 if (!stralloc_cats(&key,host)) return -9;
81 r = mffind(mf);
82 if (r < -1 || r > 0) return r;
83 }
84 }
85
86 if (!stralloc_copys(&key,ip)) return -9;
87 r = mffind(mf);
88 if (r < -1 || r > 0) return r;
89
90 if (host) {
91 if (!stralloc_copys(&key,"=")) return -9;
92 if (!stralloc_cats(&key,host)) return -9;
93 r = mffind(mf);
94 if (r < -1 || r > 0) return r;
95 }
96
97 if (!stralloc_copys(&key,ip)) return -9; /* IPv6 */
98 while (key.len > 0) {
99 if (ip[key.len - 1] == ':') {
100 r = mffind(mf);
101 if (r < -1 || r > 0) return r;
102 }
103 --key.len;
104 }
105
106 if (!stralloc_copys(&key,ip)) return -9; /* IPv4 */
107 while (key.len > 0) {
108 if (ip[key.len - 1] == '.') {
109 r = mffind(mf);
110 if (r < -1 || r > 0) return r;
111 }
112 --key.len;
113 }
114
115 if (host) {
116 while (*host) {
117 if (*host == '.') {
118 if (!stralloc_copys(&key,"=")) return -9;
119 if (!stralloc_cats(&key,host)) return -9;
120 r = mffind(mf);
121 if (r < -1 || r > 0) return r;
122 }
123 ++host;
124 }
125 if (!stralloc_copys(&key,"=")) return -9;
126 r = mffind(mf);
127 if (r < -1 || r > 0) return r;
128 }
129
130 key.len = 0;
131/* return mffind(mf); */
132 return -1;
133}
134
135int mfrules(int fd,char *ip,char *host,char *info,char *mf)
136{
137 int r;
138
139 cdb_init(&cdb,fd);
140 case_lowers(mf);
141 r = mfsearch(ip,host,info,mf);
142 cdb_free(&cdb);
143 close(fd);
144
145 return r;
146}
int stralloc_copys(stralloc *, char const *)
stralloc data
Definition: fastforward.c:118
struct cdb cdb
Definition: fastforward.c:119
uint32 dlen
Definition: fastforward.c:117
char host[256]
Definition: hostname.c:5
void info()
Definition: matchup.c:338
stralloc key
Definition: mfrules.c:17
int mfrules(int fd, char *ip, char *host, char *info, char *mf)
Definition: mfrules.c:135
int mfsearch(char *ip, char *host, char *info, char *mf)
Definition: mfrules.c:63
int fd