also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -22,6 +22,7 @@ const outbound = @import("../delivery/outbound.zig"); | ||
| 22 | 22 | const retry = @import("../delivery/retry.zig"); |
| 23 | 23 | const aliases = @import("../delivery/aliases.zig"); |
| 24 | 24 | const typesense = @import("../search/typesense.zig"); |
| 25 | const sieve = @import("../features/sieve.zig"); | |
| 25 | 26 | |
| 26 | 27 | /// Maximum number of in-flight detached outbound/forward delivery worker |
| 27 | 28 | /// threads across the whole process. Without a cap, a flood of recipients |
| @@ -2164,9 +2165,131 @@ pub const Session = struct { | ||
| 2164 | 2165 | }; |
| 2165 | 2166 | } |
| 2166 | 2167 | |
| 2167 | /// Check forwards.json for auto-forwarding rules and relay if matched. | |
| 2168 | /// Format: {"hi": ["chris@stacksjs.com"], "info": ["a@x.com","b@x.com"]} | |
| 2168 | fn forwardMessage(self: *Session, username: []const u8, sender: []const u8, data: []const u8, forward_to: []const u8) !void { | |
| 2169 | self.logger.info("Auto-forwarding from {s} to {s}", .{ username, forward_to }); | |
| 2170 | ||
| 2171 | const is_local_forward = if (std.mem.indexOf(u8, forward_to, "@")) |at_pos| | |
| 2172 | self.config.isLocalDomain(forward_to[at_pos + 1 ..]) | |
| 2173 | else | |
| 2174 | true; | |
| 2175 | ||
| 2176 | if (is_local_forward) { | |
| 2177 | const fwd_is_isolated = if (self.auth_backend) |ab| | |
| 2178 | (ab.db.userExists(forward_to) catch false) | |
| 2179 | else | |
| 2180 | false; | |
| 2181 | const fwd_username = if (fwd_is_isolated) | |
| 2182 | forward_to | |
| 2183 | else if (std.mem.indexOf(u8, forward_to, "@")) |at_pos| | |
| 2184 | aliases.resolve(forward_to[0..at_pos]) | |
| 2185 | else | |
| 2186 | aliases.resolve(forward_to); | |
| 2187 | ||
| 2188 | const cwd = fs_compat.cwd(); | |
| 2189 | const fwd_dir = try std.fmt.allocPrint(self.allocator, "mail/{s}/new", .{fwd_username}); | |
| 2190 | defer self.allocator.free(fwd_dir); | |
| 2191 | cwd.makePath(fwd_dir) catch return error.ForwardDirectoryFailed; | |
| 2192 | ||
| 2193 | var fwd_base_buf: [64]u8 = undefined; | |
| 2194 | const fwd_basename = maildirBasename(&fwd_base_buf) catch return error.ForwardNameFailed; | |
| 2195 | const fwd_filename = try std.fmt.allocPrint(self.allocator, "mail/{s}/new/{s}", .{ fwd_username, fwd_basename }); | |
| 2196 | defer self.allocator.free(fwd_filename); | |
| 2197 | ||
| 2198 | const file = cwd.createFileExclusive(fwd_filename) catch |err| { | |
| 2199 | self.logger.err("Failed to create forward file {s}: {}", .{ fwd_filename, err }); | |
| 2200 | return error.ForwardCreateFailed; | |
| 2201 | }; | |
| 2202 | file.writeAll(data) catch |err| { | |
| 2203 | file.close(); | |
| 2204 | cwd.deleteFile(fwd_filename) catch {}; | |
| 2205 | self.logger.err("Failed to write forward data to {s}: {}", .{ fwd_filename, err }); | |
| 2206 | return error.ForwardWriteFailed; | |
| 2207 | }; | |
| 2208 | file.close(); | |
| 2209 | self.logger.info("Local forward from {s} to {s} saved to {s}", .{ username, fwd_username, fwd_filename }); | |
| 2210 | return; | |
| 2211 | } | |
| 2212 | ||
| 2213 | if (!tryReserveOutboundSlot()) { | |
| 2214 | self.logger.warn("Outbound worker limit reached; queueing forward to {s}", .{forward_to}); | |
| 2215 | retry.enqueueFailure(sender, forward_to, data); | |
| 2216 | return; | |
| 2217 | } | |
| 2218 | const bg = self.allocator.create(OutboundContext) catch { | |
| 2219 | releaseOutboundSlot(); | |
| 2220 | retry.enqueueFailure(sender, forward_to, data); | |
| 2221 | return; | |
| 2222 | }; | |
| 2223 | bg.* = .{ | |
| 2224 | .allocator = self.allocator, | |
| 2225 | .from = self.allocator.dupe(u8, sender) catch { | |
| 2226 | self.allocator.destroy(bg); | |
| 2227 | releaseOutboundSlot(); | |
| 2228 | retry.enqueueFailure(sender, forward_to, data); | |
| 2229 | return; | |
| 2230 | }, | |
| 2231 | .to = self.allocator.dupe(u8, forward_to) catch { | |
| 2232 | self.allocator.free(bg.from); | |
| 2233 | self.allocator.destroy(bg); | |
| 2234 | releaseOutboundSlot(); | |
| 2235 | retry.enqueueFailure(sender, forward_to, data); | |
| 2236 | return; | |
| 2237 | }, | |
| 2238 | .data = self.allocator.dupe(u8, data) catch { | |
| 2239 | self.allocator.free(bg.from); | |
| 2240 | self.allocator.free(bg.to); | |
| 2241 | self.allocator.destroy(bg); | |
| 2242 | releaseOutboundSlot(); | |
| 2243 | retry.enqueueFailure(sender, forward_to, data); | |
| 2244 | return; | |
| 2245 | }, | |
| 2246 | .hostname = self.config.hostname, | |
| 2247 | .delivery_method = self.config.delivery_method, | |
| 2248 | .ses_region = self.config.ses_region, | |
| 2249 | }; | |
| 2250 | const thread = std.Thread.spawn(.{}, outboundWorker, .{bg}) catch |err| { | |
| 2251 | self.logger.err("Failed to spawn forward thread to {s}: {}", .{ forward_to, err }); | |
| 2252 | self.allocator.free(bg.data); | |
| 2253 | self.allocator.free(bg.to); | |
| 2254 | self.allocator.free(bg.from); | |
| 2255 | self.allocator.destroy(bg); | |
| 2256 | releaseOutboundSlot(); | |
| 2257 | retry.enqueueFailure(sender, forward_to, data); | |
| 2258 | return; | |
| 2259 | }; | |
| 2260 | thread.detach(); | |
| 2261 | } | |
| 2262 | ||
| 2263 | /// Evaluate the standard RFC 5228 forwarding script. `forwards.json` | |
| 2264 | /// remains a compatibility fallback for installations not yet migrated. | |
| 2169 | 2265 | fn checkAndForward(self: *Session, username: []const u8, sender: []const u8, data: []const u8) !void { |
| 2266 | if (fs_compat.readFileAlloc(self.allocator, "forwards.sieve")) |source| { | |
| 2267 | defer self.allocator.free(source); | |
| 2268 | var parser = sieve.SieveParser.init(self.allocator); | |
| 2269 | defer parser.deinit(); | |
| 2270 | var script = parser.parse(source) catch |err| { | |
| 2271 | self.logger.err("Invalid forwards.sieve: {}", .{err}); | |
| 2272 | return error.InvalidForwardScript; | |
| 2273 | }; | |
| 2274 | defer script.deinit(); | |
| 2275 | if (!script.is_valid) return error.InvalidForwardScript; | |
| 2276 | ||
| 2277 | var message = sieve.SieveMessage.init(self.allocator); | |
| 2278 | defer message.deinit(); | |
| 2279 | message.envelope_from = sender; | |
| 2280 | message.envelope_to = username; | |
| 2281 | ||
| 2282 | var evaluator = sieve.SieveEvaluator.init(self.allocator); | |
| 2283 | const actions = try evaluator.evaluate(&script, &message); | |
| 2284 | defer self.allocator.free(actions); | |
| 2285 | for (actions) |action| { | |
| 2286 | if (action.action_type == .redirect) { | |
| 2287 | if (action.argument) |target| try self.forwardMessage(username, sender, data, target); | |
| 2288 | } | |
| 2289 | } | |
| 2290 | return; | |
| 2291 | } else |_| {} | |
| 2292 | ||
| 2170 | 2293 | const contents = fs_compat.readFileAlloc(self.allocator, "forwards.json") catch return; |
| 2171 | 2294 | defer self.allocator.free(contents); |
| 2172 | 2295 | if (contents.len == 0) return; |