-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserthrottle.c
More file actions
177 lines (148 loc) · 4.43 KB
/
userthrottle.c
File metadata and controls
177 lines (148 loc) · 4.43 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
/*
* Copyright (c) Regents of The University of Michigan
* See COPYING.
*/
#include <errno.h>
#include <inttypes.h>
#include <string.h>
#include <strings.h>
#include <urcl.h>
#include "yasl.h"
#include "penaltybox.h"
int
main(int ac, char *av[]) {
char *redis_host = "127.0.0.1";
int redis_port = 6379;
int c;
int err = 0;
long long score;
long long total;
extern int optind;
char *endptr;
char *prefix = USERTHROTTLE_PREFIX;
yastr buf;
char *env_buf;
yastr uniqname;
yastr mailfrom;
yastr hfrom = NULL;
yastr domain = NULL;
yastr key;
urclHandle *urc;
redisReply *res;
while ((c = getopt(ac, av, "d:h:p:P:")) != -1) {
switch (c) {
case 'd':
domain = yaslauto(optarg);
yasltolower(domain);
break;
case 'h':
redis_host = optarg;
break;
case 'p':
errno = 0;
redis_port = strtoll(optarg, NULL, 10);
if (errno) {
err++;
}
break;
case 'P':
prefix = optarg;
break;
case '?':
default:
err++;
break;
}
}
if ((ac - optind) != 0) {
err++;
}
if (err) {
fprintf(stderr,
"usage: %s [ -h redis-host ] [ -p redis-port ] "
"[ -P redis-prefix ] [ -d expected-domain ]\n",
av[ 0 ]);
exit(1);
}
if ((env_buf = getenv("SIMTA_AUTH_ID")) == NULL) {
fprintf(stderr, "SIMTA_AUTH_ID not set\n");
exit(1);
}
uniqname = yaslauto(env_buf);
yasltolower(uniqname);
if ((env_buf = getenv("SIMTA_SMTP_MAIL_FROM")) == NULL) {
fprintf(stderr, "SIMTA_SMTP_MAIL_FROM not set\n");
exit(1);
}
mailfrom = yaslauto(env_buf);
yasltolower(mailfrom);
if ((env_buf = getenv("SIMTA_HEADER_FROM")) != NULL) {
hfrom = yaslauto(env_buf);
yasltolower(hfrom);
}
if ((env_buf = getenv("SIMTA_NRCPTS")) == NULL) {
fprintf(stderr, "SIMTA_NRCPTS not set\n");
exit(1);
}
errno = 0;
score = strtoll(env_buf, &endptr, 10);
if ((errno != 0) || (*endptr != '\0')) {
fprintf(stderr, "SIMTA_NRCPTS invalid\n");
exit(1);
}
if ((urc = urcl_connect(redis_host, redis_port)) == NULL) {
fprintf(stderr, "Unable to connect to %s:%d\n", redis_host, redis_port);
exit(1);
}
/* Adjust the score if the sender doesn't match the authenticated user */
buf = yasldup(mailfrom);
yaslrange(buf, 0, yasllen(uniqname) - 1);
if (yaslcmp(buf, uniqname) != 0) {
score *= 2;
}
/* Adjust the score if the addresses don't match */
if (hfrom && (yaslcmp(mailfrom, hfrom) != 0)) {
score *= 2;
}
/* Adjust the score if it's from a different domain */
if (domain) {
buf = yasldup(mailfrom);
yaslrange(buf, 0 - (yasllen(domain)), -1);
if (yaslcmp(buf, domain) != 0) {
score *= 2;
}
}
/* Adjust the score if the authuser has sent from multiple addresses */
key = yaslcatprintf(
yaslauto(prefix), ":user:%s:rfc5321.mailfrom", uniqname);
if (((res = urcl_command(
urc, key, "HINCRBY %s %s %s", key, mailfrom, "1")) == NULL) ||
(res->type != REDIS_REPLY_INTEGER)) {
fprintf(stderr, "HINCRBY on %s %s failed\n", key, mailfrom);
exit(1);
}
/* This hash never expires if it's active. */
urcl_command(urc, key, "EXPIRE %s 86400", key);
if (((res = urcl_command(urc, key, "HLEN %s", key)) == NULL) ||
(res->type != REDIS_REPLY_INTEGER)) {
fprintf(stderr, "HLEN on %s failed\n", key);
exit(1);
}
if (res->integer > 1) {
score *= (res->integer * (res->integer - 1));
}
key = yaslcatprintf(yaslauto(prefix), ":user:%s", uniqname);
if (((res = urcl_command(urc, key, "INCRBY %s %s", key,
yaslfromlonglong(score))) == NULL) ||
(res->type != REDIS_REPLY_INTEGER)) {
fprintf(stderr, "INCRBY on %s failed\n", key);
exit(1);
}
total = res->integer;
if (total == score) {
/* This is a new key, set it to expire in 24 hours */
urcl_command(urc, key, "EXPIRE %s 86400", key);
}
printf("%lld\n", total);
exit(0);
}