-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHBPBlockManager.m
More file actions
241 lines (204 loc) · 8.95 KB
/
HBPBlockManager.m
File metadata and controls
241 lines (204 loc) · 8.95 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
// HBPBlockManager.m
#import "HBPBlockManager.h"
#include <stdio.h> /* rename(2) */
#include <errno.h> /* errno, strerror() */
@implementation HBPBlockManager {
HBPConfiguration *_config;
}
- (instancetype)initWithConfiguration:(HBPConfiguration *)config {
self = [super init];
if (self) {
_config = config;
}
return self;
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
/// Return the set of IPs currently in the block file.
- (NSMutableSet<NSString *> *)loadBlockedIPs {
NSString *content = [NSString stringWithContentsOfFile:_config.blockFile
encoding:NSUTF8StringEncoding
error:nil];
NSMutableSet<NSString *> *set = [NSMutableSet set];
if (!content) { return set; }
for (NSString *line in [content componentsSeparatedByString:@"\n"]) {
NSString *ip = [line stringByTrimmingCharactersInSet:
NSCharacterSet.whitespaceAndNewlineCharacterSet];
if (ip.length > 0) { [set addObject:ip]; }
}
return set;
}
/// Append @a text (which should end with "\n") to the file at @a path.
/// Creates the file if it does not yet exist.
- (void)appendText:(NSString *)text toFile:(NSString *)path {
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
if (!data) { return; }
NSFileManager *fm = NSFileManager.defaultManager;
if (![fm fileExistsAtPath:path]) {
[fm createFileAtPath:path contents:data attributes:nil];
return;
}
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path];
if (!fh) {
NSLog(@"pf-blocker: cannot open %@ for writing: %s", path, strerror(errno));
return;
}
[fh seekToEndOfFile];
[fh writeData:data];
[fh closeFile];
}
/// Send a single message to the remote syslog server via logger(1).
/// Mirrors: logger -n HOST -P PORT -p PRIORITY MESSAGE
- (void)logToRemoteSyslog:(NSString *)message priority:(NSString *)priority {
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/usr/bin/logger";
task.arguments = @[ @"-n", _config.syslogHost,
@"-P", _config.syslogPort,
@"-p", priority,
message ];
NSError *err = nil;
if (![task launchAndReturnError:&err]) {
NSLog(@"pf-blocker: logger failed: %@", err);
} else {
[task waitUntilExit];
}
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
- (NSInteger)addBlocksForIPs:(NSArray<NSString *> *)ips
syslogTag:(NSString *)tag {
if (ips.count == 0) { return 0; }
NSMutableSet<NSString *> *blocked = [self loadBlockedIPs];
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
NSString *nowStr = [NSString stringWithFormat:@"%.0f", now];
NSInteger newBlocks = 0;
for (NSString *ip in ips) {
if ([blocked containsObject:ip]) { continue; }
// Append to block file.
[self appendText:[ip stringByAppendingString:@"\n"]
toFile:_config.blockFile];
// Append to ledger: "IP EPOCH\n"
[self appendText:[NSString stringWithFormat:@"%@ %@\n", ip, nowStr]
toFile:_config.ledgerFile];
// Remote syslog notification.
NSString *msg = [NSString stringWithFormat:@"pf-blocker: %@ %@", tag, ip];
[self logToRemoteSyslog:msg priority:@"auth.warning"];
[blocked addObject:ip];
newBlocks++;
}
return newBlocks;
}
- (NSInteger)expireOldBlocks {
NSError *err = nil;
NSString *ledgerContent = [NSString stringWithContentsOfFile:_config.ledgerFile
encoding:NSUTF8StringEncoding
error:&err];
if (!ledgerContent) {
// Nothing to do when the ledger does not exist yet.
return 0;
}
NSTimeInterval now = NSDate.date.timeIntervalSince1970;
NSTimeInterval threshold = _config.blockHours * 3600.0;
NSMutableArray<NSString *> *remaining = [NSMutableArray array];
NSMutableArray<NSString *> *expired = [NSMutableArray array];
for (NSString *line in [ledgerContent componentsSeparatedByString:@"\n"]) {
NSString *trimmed = [line stringByTrimmingCharactersInSet:
NSCharacterSet.whitespaceAndNewlineCharacterSet];
if (trimmed.length == 0) { continue; }
// Each ledger line is "IP EPOCH_SECONDS".
NSArray<NSString *> *parts = [trimmed componentsSeparatedByString:@" "];
if (parts.count < 2) { continue; }
NSString *ip = parts[0];
NSTimeInterval ts = [parts[1] doubleValue];
NSTimeInterval age = now - ts;
if (age >= threshold) {
[expired addObject:ip];
} else {
[remaining addObject:trimmed];
}
}
if (expired.count == 0) { return 0; }
// Build a new block file that excludes all expired IPs.
NSString *blockContent = [NSString stringWithContentsOfFile:_config.blockFile
encoding:NSUTF8StringEncoding
error:nil] ?: @"";
NSSet<NSString *> *expiredSet = [NSSet setWithArray:expired];
NSMutableArray<NSString *> *newBlockLines = [NSMutableArray array];
for (NSString *line in [blockContent componentsSeparatedByString:@"\n"]) {
NSString *ip = [line stringByTrimmingCharactersInSet:
NSCharacterSet.whitespaceAndNewlineCharacterSet];
if (ip.length > 0 && ![expiredSet containsObject:ip]) {
[newBlockLines addObject:ip];
}
}
// Write to temp files in the same directory so rename(2) is atomic.
NSString *dir = [_config.blockFile stringByDeletingLastPathComponent];
NSString *pid = [NSString stringWithFormat:@"%d",
(int)NSProcessInfo.processInfo.processIdentifier];
NSString *tmpBlock = [dir stringByAppendingPathComponent:
[@"arbitraryBlocks.tmp." stringByAppendingString:pid]];
NSString *tmpLedger = [dir stringByAppendingPathComponent:
[@"blockLedger.tmp." stringByAppendingString:pid]];
NSString *newBlockContent =
newBlockLines.count > 0
? [[newBlockLines componentsJoinedByString:@"\n"] stringByAppendingString:@"\n"]
: @"";
NSString *newLedgerContent =
remaining.count > 0
? [[remaining componentsJoinedByString:@"\n"] stringByAppendingString:@"\n"]
: @"";
NSError *writeErr = nil;
if (![newBlockContent writeToFile:tmpBlock
atomically:NO
encoding:NSUTF8StringEncoding
error:&writeErr]) {
NSLog(@"pf-blocker: cannot write temp block file: %@", writeErr);
return 0;
}
if (![newLedgerContent writeToFile:tmpLedger
atomically:NO
encoding:NSUTF8StringEncoding
error:&writeErr]) {
NSLog(@"pf-blocker: cannot write temp ledger file: %@", writeErr);
[[NSFileManager defaultManager] removeItemAtPath:tmpBlock error:nil];
return 0;
}
// rename(2) atomically replaces the destination on the same filesystem.
if (rename(tmpBlock.fileSystemRepresentation,
_config.blockFile.fileSystemRepresentation) != 0) {
perror("pf-blocker: rename block file");
[[NSFileManager defaultManager] removeItemAtPath:tmpBlock error:nil];
[[NSFileManager defaultManager] removeItemAtPath:tmpLedger error:nil];
return 0;
}
if (rename(tmpLedger.fileSystemRepresentation,
_config.ledgerFile.fileSystemRepresentation) != 0) {
perror("pf-blocker: rename ledger file");
[[NSFileManager defaultManager] removeItemAtPath:tmpLedger error:nil];
return 0;
}
// Log each expiry to the remote syslog server.
for (NSString *ip in expired) {
NSString *msg = [NSString stringWithFormat:
@"pf-blocker: expired block for %@ after %ldh",
ip, (long)_config.blockHours];
[self logToRemoteSyslog:msg priority:@"auth.info"];
}
return (NSInteger)expired.count;
}
- (void)reloadPFTable {
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/sbin/pfctl";
task.arguments = @[ @"-t", _config.pfTableName,
@"-T", @"replace",
@"-f", _config.blockFile ];
NSError *err = nil;
if (![task launchAndReturnError:&err]) {
NSLog(@"pf-blocker: pfctl failed: %@", err);
} else {
[task waitUntilExit];
}
}
@end