also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -17,6 +17,8 @@ const typesense = @import("../search/typesense.zig"); | ||
| 17 | 17 | // std.posix.getpid is not available on this Zig version's std; libc is linked |
| 18 | 18 | // (the binary links sqlite3 + libc), so declare the C symbol directly. |
| 19 | 19 | extern "c" fn getpid() c_int; |
| 20 | extern "c" fn readlink(path: [*:0]const u8, buf: [*]u8, bufsiz: usize) isize; | |
| 21 | extern "c" fn symlink(target: [*:0]const u8, linkpath: [*:0]const u8) c_int; | |
| 20 | 22 | |
| 21 | 23 | /// Upper bound on an APPEND literal we are willing to buffer in memory, to |
| 22 | 24 | /// prevent a client from exhausting server memory with a huge {N} literal. |
| @@ -794,6 +796,29 @@ fn renameFile(old_path: []const u8, new_path: []const u8) bool { | ||
| 794 | 796 | return std.c.rename(old_z, new_z) == 0; |
| 795 | 797 | } |
| 796 | 798 | |
| 799 | fn pathIsSymlink(path: []const u8) bool { | |
| 800 | var path_buf: [4097]u8 = undefined; | |
| 801 | var target_buf: [1]u8 = undefined; | |
| 802 | if (path.len >= path_buf.len) return false; | |
| 803 | @memcpy(path_buf[0..path.len], path); | |
| 804 | path_buf[path.len] = 0; | |
| 805 | const path_z: [*:0]const u8 = @ptrCast(&path_buf); | |
| 806 | return readlink(path_z, &target_buf, target_buf.len) >= 0; | |
| 807 | } | |
| 808 | ||
| 809 | fn createRelativeSymlink(target: []const u8, path: []const u8) bool { | |
| 810 | var target_buf: [4097]u8 = undefined; | |
| 811 | var path_buf: [4097]u8 = undefined; | |
| 812 | if (target.len >= target_buf.len or path.len >= path_buf.len) return false; | |
| 813 | @memcpy(target_buf[0..target.len], target); | |
| 814 | target_buf[target.len] = 0; | |
| 815 | @memcpy(path_buf[0..path.len], path); | |
| 816 | path_buf[path.len] = 0; | |
| 817 | const target_z: [*:0]const u8 = @ptrCast(&target_buf); | |
| 818 | const path_z: [*:0]const u8 = @ptrCast(&path_buf); | |
| 819 | return symlink(target_z, path_z) == 0; | |
| 820 | } | |
| 821 | ||
| 797 | 822 | /// IMAP session |
| 798 | 823 | pub const ImapSession = struct { |
| 799 | 824 | allocator: std.mem.Allocator, |
| @@ -3361,6 +3386,51 @@ pub const ImapSession = struct { | ||
| 3361 | 3386 | _ = std.c.unlink(@ptrCast(&path_buf)); |
| 3362 | 3387 | } |
| 3363 | 3388 | |
| 3389 | if (pathIsSymlink(dir_path)) { | |
| 3390 | var visible_buf: [4097]u8 = undefined; | |
| 3391 | const visible = std.fmt.bufPrint(&visible_buf, "{s}", .{dir_path}) catch { | |
| 3392 | try self.sendResponse(tag, "NO", "DELETE failed"); | |
| 3393 | return; | |
| 3394 | }; | |
| 3395 | visible_buf[visible.len] = 0; | |
| 3396 | _ = std.c.unlink(@ptrCast(&visible_buf)); | |
| 3397 | ||
| 3398 | const canonical = try std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local_part, name }); | |
| 3399 | defer self.allocator.free(canonical); | |
| 3400 | for ([_][]const u8{ "tmp", "new", "cur" }) |component| { | |
| 3401 | const child = try std.fmt.allocPrint(self.allocator, "{s}/{s}", .{ canonical, component }); | |
| 3402 | defer self.allocator.free(child); | |
| 3403 | const child_files = fs_compat.listEmlFiles(self.allocator, child) catch &.{}; | |
| 3404 | defer { | |
| 3405 | for (child_files) |file_name| self.allocator.free(file_name); | |
| 3406 | if (child_files.len > 0) self.allocator.free(child_files); | |
| 3407 | } | |
| 3408 | for (child_files) |file_name| { | |
| 3409 | var child_file_buf: [4097]u8 = undefined; | |
| 3410 | const child_file = std.fmt.bufPrint(&child_file_buf, "{s}/{s}", .{ child, file_name }) catch continue; | |
| 3411 | child_file_buf[child_file.len] = 0; | |
| 3412 | _ = std.c.unlink(@ptrCast(&child_file_buf)); | |
| 3413 | } | |
| 3414 | var child_buf: [4097]u8 = undefined; | |
| 3415 | const child_path = std.fmt.bufPrint(&child_buf, "{s}", .{child}) catch continue; | |
| 3416 | child_buf[child_path.len] = 0; | |
| 3417 | _ = std.c.rmdir(@ptrCast(&child_buf)); | |
| 3418 | } | |
| 3419 | var canonical_buf: [4097]u8 = undefined; | |
| 3420 | const canonical_path = std.fmt.bufPrint(&canonical_buf, "{s}", .{canonical}) catch { | |
| 3421 | try self.sendResponse(tag, "NO", "DELETE failed"); | |
| 3422 | return; | |
| 3423 | }; | |
| 3424 | canonical_buf[canonical_path.len] = 0; | |
| 3425 | if (std.c.rmdir(@ptrCast(&canonical_buf)) != 0) { | |
| 3426 | try self.sendResponse(tag, "NO", "DELETE failed"); | |
| 3427 | return; | |
| 3428 | } | |
| 3429 | std.log.info("IMAP DELETE: Removed Maildir++ mailbox {s}", .{name}); | |
| 3430 | try self.sendResponse(tag, "OK", "DELETE completed"); | |
| 3431 | return; | |
| 3432 | } | |
| 3433 | ||
| 3364 | 3434 | var dir_buf: [4097]u8 = undefined; |
| 3365 | 3435 | const dir_written = std.fmt.bufPrint(&dir_buf, "{s}", .{dir_path}) catch { |
| 3366 | 3436 | try self.sendResponse(tag, "OK", "DELETE completed"); |
| @@ -3404,6 +3474,39 @@ pub const ImapSession = struct { | ||
| 3404 | 3474 | const new_path = try std.fmt.allocPrint(self.allocator, "mail/{s}/{s}", .{ local_part, new }); |
| 3405 | 3475 | defer self.allocator.free(new_path); |
| 3406 | 3476 | |
| 3477 | if (pathIsSymlink(old_path)) { | |
| 3478 | const old_canonical = try std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local_part, old }); | |
| 3479 | defer self.allocator.free(old_canonical); | |
| 3480 | const new_canonical = try std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local_part, new }); | |
| 3481 | defer self.allocator.free(new_canonical); | |
| 3482 | const new_target = try std.fmt.allocPrint(self.allocator, ".{s}/cur", .{new}); | |
| 3483 | defer self.allocator.free(new_target); | |
| 3484 | const old_target = try std.fmt.allocPrint(self.allocator, ".{s}/cur", .{old}); | |
| 3485 | defer self.allocator.free(old_target); | |
| 3486 | ||
| 3487 | if (!renameFile(old_canonical, new_canonical)) { | |
| 3488 | try self.sendResponse(tag, "NO", "RENAME failed"); | |
| 3489 | return; | |
| 3490 | } | |
| 3491 | var old_buf: [4097]u8 = undefined; | |
| 3492 | const old_visible = std.fmt.bufPrint(&old_buf, "{s}", .{old_path}) catch { | |
| 3493 | _ = renameFile(new_canonical, old_canonical); | |
| 3494 | try self.sendResponse(tag, "NO", "RENAME failed"); | |
| 3495 | return; | |
| 3496 | }; | |
| 3497 | old_buf[old_visible.len] = 0; | |
| 3498 | _ = std.c.unlink(@ptrCast(&old_buf)); | |
| 3499 | if (!createRelativeSymlink(new_target, new_path)) { | |
| 3500 | _ = renameFile(new_canonical, old_canonical); | |
| 3501 | _ = createRelativeSymlink(old_target, old_path); | |
| 3502 | try self.sendResponse(tag, "NO", "RENAME failed"); | |
| 3503 | return; | |
| 3504 | } | |
| 3505 | std.log.info("IMAP RENAME: {s} -> {s}", .{ old, new }); | |
| 3506 | try self.sendResponse(tag, "OK", "RENAME completed"); | |
| 3507 | return; | |
| 3508 | } | |
| 3509 | ||
| 3407 | 3510 | if (renameFile(old_path, new_path)) { |
| 3408 | 3511 | std.log.info("IMAP RENAME: {s} -> {s}", .{ old, new }); |
| 3409 | 3512 | try self.sendResponse(tag, "OK", "RENAME completed"); |
| @@ -3859,21 +3962,42 @@ pub const ImapSession = struct { | ||
| 3859 | 3962 | try self.sendResponse(tag, "OK", "ID completed"); |
| 3860 | 3963 | }, |
| 3861 | 3964 | .create => { |
| 3862 | // CREATE mailbox - create the Maildir folder | |
| 3965 | // CREATE mailbox in canonical Maildir++ form. The visible | |
| 3966 | // folder symlink preserves compatibility with older server | |
| 3967 | // paths while external Maildir tools use .Folder/{tmp,new,cur}. | |
| 3863 | 3968 | const create_name = stripQuotes(parts.next() orelse ""); |
| 3864 | 3969 | if (!isSafeMailboxName(create_name)) { |
| 3865 | 3970 | // Rejects empty names and any path-traversal attempt. |
| 3866 | 3971 | try self.sendResponse(tag, "NO", "Invalid mailbox name"); |
| 3867 | 3972 | } else { |
| 3973 | var created = false; | |
| 3868 | 3974 | if (self.username) |uname| { |
| 3869 | 3975 | const local = uname; |
| 3870 | const dir = std.fmt.allocPrint(self.allocator, "mail/{s}/{s}", .{ local, create_name }) catch null; | |
| 3871 | if (dir) |d| { | |
| 3872 | fs_compat.cwd().makePath(d) catch {}; | |
| 3873 | self.allocator.free(d); | |
| 3976 | const visible = std.fmt.allocPrint(self.allocator, "mail/{s}/{s}", .{ local, create_name }) catch null; | |
| 3977 | const canonical = std.fmt.allocPrint(self.allocator, "mail/{s}/.{s}", .{ local, create_name }) catch null; | |
| 3978 | const target = std.fmt.allocPrint(self.allocator, ".{s}/cur", .{create_name}) catch null; | |
| 3979 | if (visible != null and canonical != null and target != null) { | |
| 3980 | const tmp = std.fmt.allocPrint(self.allocator, "{s}/tmp", .{canonical.?}) catch null; | |
| 3981 | const new = std.fmt.allocPrint(self.allocator, "{s}/new", .{canonical.?}) catch null; | |
| 3982 | const cur = std.fmt.allocPrint(self.allocator, "{s}/cur", .{canonical.?}) catch null; | |
| 3983 | if (tmp != null and new != null and cur != null) { | |
| 3984 | fs_compat.cwd().makePath(tmp.?) catch {}; | |
| 3985 | fs_compat.cwd().makePath(new.?) catch {}; | |
| 3986 | fs_compat.cwd().makePath(cur.?) catch {}; | |
| 3987 | created = createRelativeSymlink(target.?, visible.?); | |
| 3988 | } | |
| 3989 | if (tmp) |value| self.allocator.free(value); | |
| 3990 | if (new) |value| self.allocator.free(value); | |
| 3991 | if (cur) |value| self.allocator.free(value); | |
| 3874 | 3992 | } |
| 3993 | if (visible) |value| self.allocator.free(value); | |
| 3994 | if (canonical) |value| self.allocator.free(value); | |
| 3995 | if (target) |value| self.allocator.free(value); | |
| 3875 | 3996 | } |
| 3876 | try self.sendResponse(tag, "OK", "CREATE completed"); | |
| 3997 | if (created) | |
| 3998 | try self.sendResponse(tag, "OK", "CREATE completed") | |
| 3999 | else | |
| 4000 | try self.sendResponse(tag, "NO", "Mailbox already exists or could not be created"); | |
| 3877 | 4001 | } |
| 3878 | 4002 | }, |
| 3879 | 4003 | .subscribe => { |